Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxies command as "light proxy" #187

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmd/atmos/atmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
package main

import (
"github.com/tofuutils/tenv/v2/config"
"github.com/tofuutils/tenv/v2/versionmanager/builder"
"github.com/tofuutils/tenv/v2/versionmanager/proxy"
"github.com/tofuutils/tenv/v2/config/cmdconst"
lightproxy "github.com/tofuutils/tenv/v2/versionmanager/proxy/light"
)

func main() {
proxy.Exec(builder.BuildAtmosManager, config.AtmosName)
lightproxy.Exec(cmdconst.AtmosName)
}
53 changes: 39 additions & 14 deletions cmd/tenv/tenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ import (
"strings"

"github.com/tofuutils/tenv/v2/config"
"github.com/tofuutils/tenv/v2/config/cmdconst"
configutils "github.com/tofuutils/tenv/v2/config/utils"
"github.com/tofuutils/tenv/v2/versionmanager"
"github.com/tofuutils/tenv/v2/versionmanager/builder"
"github.com/tofuutils/tenv/v2/versionmanager/proxy"
terragruntparser "github.com/tofuutils/tenv/v2/versionmanager/semantic/parser/terragrunt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -64,15 +67,25 @@ func main() {
os.Exit(1)
}

if err = initRootCmd(&conf).Execute(); err != nil {
builders := map[string]builder.BuilderFunc{
cmdconst.TofuName: builder.BuildTofuManager,
cmdconst.TerraformName: builder.BuildTfManager,
cmdconst.TerragruntName: builder.BuildTgManager,
cmdconst.AtmosName: builder.BuildAtmosManager,
}

gruntParser := terragruntparser.Make()
manageHiddenCallCmd(&conf, builders, gruntParser) // proxy call use os.Exit when called

if err = initRootCmd(&conf, builders, gruntParser).Execute(); err != nil {
fmt.Println(err) //nolint
os.Exit(1)
}
}

func initRootCmd(conf *config.Config) *cobra.Command {
func initRootCmd(conf *config.Config, builders map[string]builder.BuilderFunc, gruntParser terragruntparser.TerragruntParser) *cobra.Command {
rootCmd := &cobra.Command{
Use: config.TenvName,
Use: cmdconst.TenvName,
Long: "tenv help manage several versions of OpenTofu (https://opentofu.org), Terraform (https://www.terraform.io), Terragrunt (https://terragrunt.gruntwork.io), and Atmos (https://atmos.tools/).",
Version: version,
}
Expand All @@ -90,12 +103,11 @@ func initRootCmd(conf *config.Config) *cobra.Command {
needToken: true, remoteEnvName: config.TofuRemoteURLEnvName,
pRemote: &conf.Tofu.RemoteURL, pPublicKeyPath: &conf.TofuKeyPath,
}
gruntParser := terragruntparser.Make()
tofuManager := builder.BuildTofuManager(conf, gruntParser)
tofuManager := builders[cmdconst.TofuName](conf, gruntParser)
initSubCmds(rootCmd, conf, tofuManager, tofuParams) // add tofu management at root level

tofuCmd := &cobra.Command{
Use: config.TofuName,
Use: cmdconst.TofuName,
Aliases: []string{"opentofu"},
Short: tofuHelp,
Long: tofuHelp,
Expand All @@ -107,7 +119,7 @@ func initRootCmd(conf *config.Config) *cobra.Command {

tfCmd := &cobra.Command{
Use: "tf",
Aliases: []string{config.TerraformName},
Aliases: []string{cmdconst.TerraformName},
Short: tfHelp,
Long: tfHelp,
}
Expand All @@ -116,48 +128,61 @@ func initRootCmd(conf *config.Config) *cobra.Command {
needToken: false, remoteEnvName: config.TfRemoteURLEnvName,
pRemote: &conf.Tf.RemoteURL, pPublicKeyPath: &conf.TfKeyPath,
}
initSubCmds(tfCmd, conf, builder.BuildTfManager(conf, gruntParser), tfParams)
initSubCmds(tfCmd, conf, builders[cmdconst.TerraformName](conf, gruntParser), tfParams)

rootCmd.AddCommand(tfCmd)

tgCmd := &cobra.Command{
Use: "tg",
Aliases: []string{config.TerragruntName},
Aliases: []string{cmdconst.TerragruntName},
Short: tgHelp,
Long: tgHelp,
}

tgParams := subCmdParams{
needToken: true, remoteEnvName: config.TgRemoteURLEnvName, pRemote: &conf.Tg.RemoteURL,
}
initSubCmds(tgCmd, conf, builder.BuildTgManager(conf, gruntParser), tgParams)
initSubCmds(tgCmd, conf, builders[cmdconst.TerragruntName](conf, gruntParser), tgParams)

rootCmd.AddCommand(tgCmd)

atmosCmd := &cobra.Command{
Use: config.AtmosName,
Use: cmdconst.AtmosName,
Short: atmosHelp,
Long: atmosHelp,
}

atmosParams := subCmdParams{
needToken: true, remoteEnvName: config.AtmosRemoteURLEnvName, pRemote: &conf.Atmos.RemoteURL,
}
initSubCmds(atmosCmd, conf, builder.BuildAtmosManager(conf, gruntParser), atmosParams)
initSubCmds(atmosCmd, conf, builders[cmdconst.AtmosName](conf, gruntParser), atmosParams)

rootCmd.AddCommand(atmosCmd)

return rootCmd
}

func manageHiddenCallCmd(conf *config.Config, builders map[string]builder.BuilderFunc, gruntParser terragruntparser.TerragruntParser) {
if len(os.Args) < 3 || os.Args[1] != cmdconst.CallSubCmd {
return
}

calledNamed, cmdArgs := os.Args[2], os.Args[3:]
if builder, ok := builders[calledNamed]; ok {
proxy.Exec(conf, builder, gruntParser, calledNamed, cmdArgs)
} else if calledNamed == cmdconst.AgnosticName {
proxy.ExecAgnostic(conf, builders, gruntParser, cmdArgs)
}
}

func newVersionCmd() *cobra.Command {
return &cobra.Command{
Use: versionName,
Short: rootVersionHelp,
Long: rootVersionHelp,
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
fmt.Println(config.TenvName, versionName, version) //nolint
fmt.Println(cmdconst.TenvName, versionName, version) //nolint
},
}
}
Expand All @@ -174,7 +199,7 @@ func newUpdatePathCmd() *cobra.Command {
return nil
}

gha, err := config.GetenvBool(false, config.GithubActionsEnvName)
gha, err := configutils.GetenvBool(false, cmdconst.GithubActionsEnvName)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
package main

import (
"github.com/tofuutils/tenv/v2/config"
"github.com/tofuutils/tenv/v2/versionmanager/builder"
"github.com/tofuutils/tenv/v2/versionmanager/proxy"
"github.com/tofuutils/tenv/v2/config/cmdconst"
lightproxy "github.com/tofuutils/tenv/v2/versionmanager/proxy/light"
)

func main() {
proxy.Exec(builder.BuildTfManager, config.TerraformName)
lightproxy.Exec(cmdconst.TerraformName)
}
7 changes: 3 additions & 4 deletions cmd/terragrunt/terragrunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
package main

import (
"github.com/tofuutils/tenv/v2/config"
"github.com/tofuutils/tenv/v2/versionmanager/builder"
"github.com/tofuutils/tenv/v2/versionmanager/proxy"
"github.com/tofuutils/tenv/v2/config/cmdconst"
lightproxy "github.com/tofuutils/tenv/v2/versionmanager/proxy/light"
)

func main() {
proxy.Exec(builder.BuildTgManager, config.TerragruntName)
lightproxy.Exec(cmdconst.TerragruntName)
}
54 changes: 3 additions & 51 deletions cmd/tf/tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,58 +19,10 @@
package main

import (
"fmt"
"os"

"github.com/tofuutils/tenv/v2/config"
"github.com/tofuutils/tenv/v2/versionmanager/builder"
"github.com/tofuutils/tenv/v2/versionmanager/proxy"
terragruntparser "github.com/tofuutils/tenv/v2/versionmanager/semantic/parser/terragrunt"
"github.com/tofuutils/tenv/v2/config/cmdconst"
lightproxy "github.com/tofuutils/tenv/v2/versionmanager/proxy/light"
)

func main() {
conf, err := config.InitConfigFromEnv()
if err != nil {
fmt.Println("Configuration error :", err) //nolint
os.Exit(1)
}

conf.InitDisplayer(true)
gruntParser := terragruntparser.Make()
manager := builder.BuildTofuManager(&conf, gruntParser)
detectedVersion, err := manager.ResolveWithVersionFiles()
if err != nil {
fmt.Println("Failed to resolve a version allowing to call tofu :", err) //nolint
os.Exit(1)
}

execName := config.TofuName
if detectedVersion == "" {
execName = config.TerraformName
manager = builder.BuildTfManager(&conf, gruntParser)
detectedVersion, err = manager.ResolveWithVersionFiles()
if err != nil {
fmt.Println("Failed to resolve a version allowing to call terraform :", err) //nolint
os.Exit(1)
}

if detectedVersion == "" {
fmt.Println("No version files found corresponding to opentofu or terraform") //nolint
os.Exit(1)
}
}

installPath, err := manager.InstallPath()
if err != nil {
fmt.Println("Failed to create installation directory for", execName, ":", err) //nolint
os.Exit(1)
}

detectedVersion, err = manager.Evaluate(detectedVersion, true)
if err != nil {
fmt.Println("Failed to evaluate the requested version in a specific version allowing to call", execName, ":", err) //nolint
os.Exit(1)
}

proxy.RunCmd(installPath, detectedVersion, execName)
lightproxy.Exec(cmdconst.AgnosticName)
}
7 changes: 3 additions & 4 deletions cmd/tofu/tofu.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
package main

import (
"github.com/tofuutils/tenv/v2/config"
"github.com/tofuutils/tenv/v2/versionmanager/builder"
"github.com/tofuutils/tenv/v2/versionmanager/proxy"
"github.com/tofuutils/tenv/v2/config/cmdconst"
lightproxy "github.com/tofuutils/tenv/v2/versionmanager/proxy/light"
)

func main() {
proxy.Exec(builder.BuildTofuManager, config.TofuName)
lightproxy.Exec(cmdconst.TofuName)
}
32 changes: 32 additions & 0 deletions config/cmdconst/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
*
* Copyright 2024 tofuutils authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package cmdconst

const (
AgnosticName = "tf"
AtmosName = "atmos"
TenvName = "tenv"
TerraformName = "terraform"
TerragruntName = "terragrunt"
TofuName = "tofu"

CallSubCmd = "call"

GithubActionsEnvName = "GITHUB_ACTIONS"
)
Loading
Loading