Skip to content

Commit

Permalink
Read config file only for "generate" command
Browse files Browse the repository at this point in the history
  • Loading branch information
aspettl committed Jan 30, 2022
1 parent 72313f8 commit 01b040f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 44 deletions.
40 changes: 40 additions & 0 deletions cfg/cfg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cfg

import (
"runtime"
"strings"
)

Expand Down Expand Up @@ -107,3 +108,42 @@ func (commandType CommandType) IsDoNotSpecify() bool {
func (commandType CommandType) IsReuseName() bool {
return strings.EqualFold(string(commandType), ReuseName)
}

func (config Config) ApplyToolDefaults() {
for toolName, toolConfig := range config.Tools {
if !toolConfig.Type.IsWrapperScript() && !toolConfig.Type.IsAlias() {
toolConfig.Type = WrapperScript
}
if toolConfig.ImageName == "" {
toolConfig.ImageName = "undefined"
}
if !toolConfig.ImageTag.Type.IsFixed() && !toolConfig.ImageTag.Type.IsFromFile() {
toolConfig.ImageTag.Type = Fixed
}
if toolConfig.ImageTag.Value == "" {
toolConfig.ImageTag.Value = "latest"
}
if toolConfig.ImageTag.File == "" {
toolConfig.ImageTag.File = "undefined"
}
if toolConfig.ImageTag.Fallback == "" {
toolConfig.ImageTag.Fallback = "latest"
}
if toolConfig.WorkDir == "" {
toolConfig.WorkDir = "/work"
}
if toolConfig.HomeDir == "" {
toolConfig.HomeDir = "/home/container"
}
if !toolConfig.Command.Type.IsDoNotSpecify() && !toolConfig.Command.Type.IsReuseName() {
toolConfig.Command.Type = DoNotSpecify
}
if toolConfig.AliasFor == "" {
toolConfig.AliasFor = "undefined"
}
if runtime.GOOS == "windows" {
toolConfig.ForceTemplate = true
}
config.Tools[toolName] = toolConfig
}
}
4 changes: 3 additions & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ var generateCmd = &cobra.Command{
Short: "Generate wrapper scripts",
Long: `Generate wrapper scripts for all configured CLI tools.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Using config file:", viper.ConfigFileUsed())
if err := readConfig(); err != nil {
cobra.CheckErr(err)
}

outputDir := expandPath(config.OutputDir)
if err := os.MkdirAll(outputDir, 0755); err != nil {
Expand Down
62 changes: 19 additions & 43 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"os"
"runtime"

"github.com/aspettl/ccliwrapper/cfg"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -61,9 +60,9 @@ func init() {
viper.SetDefault("Tools", map[string]cfg.ToolConfig{})
}

// initConfig reads in config file and ENV variables if set.
// initConfig sets up the config file in viper, but does not actually try to read.
func initConfig() {
// Use yaml as file format when file name does not have a file extension
// Use yaml as file format when file name does not have a file extension.
viper.SetConfigType("yaml")

if cfgFile != "" {
Expand All @@ -74,51 +73,28 @@ func initConfig() {
viper.AddConfigPath(homeDir)
viper.SetConfigName(cfgFileDefault)
}
}

// If a config file is found, read it in.
// readConfig reads the config file, this method needs to be explicitly called by all the commands needing the config.
func readConfig() error {
// Read config file.
err := viper.ReadInConfig()
cobra.CheckErr(err)
if viper.ConfigFileUsed() != "" {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
if err != nil {
return err
}

// Parse the data into our config struct.
err = viper.Unmarshal(&config)
cobra.CheckErr(err)
if err := viper.Unmarshal(&config); err != nil {
return err
}

// Apply some default values for configured tools.
for toolName, toolConfig := range config.Tools {
if !toolConfig.Type.IsWrapperScript() && !toolConfig.Type.IsAlias() {
toolConfig.Type = cfg.WrapperScript
}
if toolConfig.ImageName == "" {
toolConfig.ImageName = "undefined"
}
if !toolConfig.ImageTag.Type.IsFixed() && !toolConfig.ImageTag.Type.IsFromFile() {
toolConfig.ImageTag.Type = cfg.Fixed
}
if toolConfig.ImageTag.Value == "" {
toolConfig.ImageTag.Value = "latest"
}
if toolConfig.ImageTag.File == "" {
toolConfig.ImageTag.File = "undefined"
}
if toolConfig.ImageTag.Fallback == "" {
toolConfig.ImageTag.Fallback = "latest"
}
if toolConfig.WorkDir == "" {
toolConfig.WorkDir = "/work"
}
if toolConfig.HomeDir == "" {
toolConfig.HomeDir = "/home/container"
}
if !toolConfig.Command.Type.IsDoNotSpecify() && !toolConfig.Command.Type.IsReuseName() {
toolConfig.Command.Type = cfg.DoNotSpecify
}
if toolConfig.AliasFor == "" {
toolConfig.AliasFor = "undefined"
}
if runtime.GOOS == "windows" {
toolConfig.ForceTemplate = true
}
config.Tools[toolName] = toolConfig
}
config.ApplyToolDefaults()

return nil
}

func fileExists(path string) bool {
Expand Down

0 comments on commit 01b040f

Please sign in to comment.