Skip to content

Commit

Permalink
Proxies command as "light proxy" (#187)
Browse files Browse the repository at this point in the history
* lightweight proxy #183
* split package to lighter proxy #183

Signed-off-by: Denis Vaumoron <[email protected]>
  • Loading branch information
dvaumoron authored Jun 26, 2024
1 parent 13dac66 commit 3af9c22
Show file tree
Hide file tree
Showing 19 changed files with 444 additions and 287 deletions.
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

0 comments on commit 3af9c22

Please sign in to comment.