diff --git a/internal/config/config.go b/internal/config/config.go index cace9186..7c79399f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,16 +3,21 @@ package config import ( "bufio" "bytes" + "crypto/tls" "fmt" "io" "io/ioutil" "log" + "net/http" "net/url" "os" + "path" "strings" "syscall" "github.com/spf13/viper" + gitlab "github.com/xanzy/go-gitlab" + "github.com/zaquestion/lab/internal/git" "golang.org/x/crypto/ssh/terminal" ) @@ -142,3 +147,99 @@ func ConvertHCLtoTOML(oldpath string, newpath string, file string) { fmt.Println("INFO: Converted old config", oldconfig, "to new config", newconfig) } + +func getUser(host, token string, skipVerify bool) string { + httpClient := &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: skipVerify, + }, + }, + } + lab, _ := gitlab.NewClient(token, gitlab.WithHTTPClient(httpClient), gitlab.WithBaseURL(host+"/api/v4")) + u, _, err := lab.Users.CurrentUser() + if err != nil { + log.Fatal(err) + } + return u.Username +} + +// LoadConfig() loads the main config file +func LoadConfig() (string, string, string, bool) { + + // Attempt to auto-configure for GitLab CI. + // Always do this before reading in the config file o/w CI will end up + // with the wrong data. + host, user, token := CI() + if host != "" && user != "" && token != "" { + return host, user, token, false + } + + // Try to find XDG_CONFIG_HOME which is declared in XDG base directory + // specification and use it's location as the config directory + home, err := os.UserHomeDir() + if err != nil { + log.Fatal(err) + } + confpath := os.Getenv("XDG_CONFIG_HOME") + if confpath == "" { + confpath = path.Join(home, ".config") + } + labconfpath := confpath + "/lab" + if _, err := os.Stat(labconfpath); os.IsNotExist(err) { + os.MkdirAll(labconfpath, 0700) + } + + // Convert old hcl files to toml format. + // NO NEW FILES SHOULD BE ADDED BELOW. + ConvertHCLtoTOML(".", ".", "lab") + ConvertHCLtoTOML(confpath, labconfpath, "lab") + var labgitDir string + gitDir, err := git.GitDir() + if err == nil { + labgitDir = gitDir + "/lab" + ConvertHCLtoTOML(gitDir, labgitDir, "lab") + ConvertHCLtoTOML(labgitDir, labgitDir, "show_metadata") + } + + viper.SetConfigName("lab") + viper.SetConfigType("toml") + viper.AddConfigPath(".") + viper.AddConfigPath(labconfpath) + if labgitDir != "" { + viper.AddConfigPath(labgitDir) + } + + viper.SetEnvPrefix("LAB") + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.AutomaticEnv() + + if _, ok := viper.ReadInConfig().(viper.ConfigFileNotFoundError); ok { + err := New(path.Join(labconfpath, "lab.toml"), os.Stdin) + if err != nil { + log.Fatal(err) + } + + err = viper.ReadInConfig() + if err != nil { + log.Fatal(err) + } + } + + host = viper.GetString("core.host") + user = viper.GetString("core.user") + token = viper.GetString("core.token") + tlsSkipVerify := viper.GetBool("tls.skip_verify") + + if host != "" && user != "" && token != "" { + return host, user, token, tlsSkipVerify + } + + user = getUser(host, token, tlsSkipVerify) + if strings.TrimSpace(os.Getenv("LAB_CORE_TOKEN")) == "" && strings.TrimSpace(os.Getenv("LAB_CORE_HOST")) == "" { + viper.Set("core.user", user) + viper.WriteConfig() + } + + return host, user, token, tlsSkipVerify +} diff --git a/main.go b/main.go index 27267cb7..5cb2775f 100644 --- a/main.go +++ b/main.go @@ -1,104 +1,19 @@ package main import ( - "crypto/tls" "log" - "net/http" "os" - "path" - "strings" "github.com/rsteube/carapace" "github.com/spf13/viper" - gitlab "github.com/xanzy/go-gitlab" "github.com/zaquestion/lab/cmd" "github.com/zaquestion/lab/internal/config" - "github.com/zaquestion/lab/internal/git" lab "github.com/zaquestion/lab/internal/gitlab" ) // version gets set on releases during build by goreleaser. var version = "master" -func loadConfig() (string, string, string, bool) { - - // Attempt to auto-configure for GitLab CI. - // Always do this before reading in the config file o/w CI will end up - // with the wrong data. - host, user, token := config.CI() - if host != "" && user != "" && token != "" { - return host, user, token, false - } - - // Try to find XDG_CONFIG_HOME which is declared in XDG base directory - // specification and use it's location as the config directory - home, err := os.UserHomeDir() - if err != nil { - log.Fatal(err) - } - confpath := os.Getenv("XDG_CONFIG_HOME") - if confpath == "" { - confpath = path.Join(home, ".config") - } - labconfpath := confpath+"/lab" - if _, err := os.Stat(confpath); os.IsNotExist(err) { - os.MkdirAll(confpath, 0700) - } - - // Convert old hcl files to toml format. - // NO NEW FILES SHOULD BE ADDED BELOW. - config.ConvertHCLtoTOML(".", ".", "lab") - config.ConvertHCLtoTOML(confpath, labconfpath, "lab") - var labgitDir string - gitDir, err := git.GitDir() - if err == nil { - labgitDir = gitDir+"/lab" - config.ConvertHCLtoTOML(gitDir, labgitDir, "lab") - config.ConvertHCLtoTOML(labgitDir, labgitDir, "show_metadata") - } - - viper.SetConfigName("lab") - viper.SetConfigType("toml") - viper.AddConfigPath(".") - viper.AddConfigPath(labconfpath) - if labgitDir != "" { - viper.AddConfigPath(labgitDir) - } - - viper.SetEnvPrefix("LAB") - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - viper.AutomaticEnv() - - if _, ok := viper.ReadInConfig().(viper.ConfigFileNotFoundError); ok { - err := config.New(path.Join(labconfpath, "lab.toml"), os.Stdin) - if err != nil { - log.Fatal(err) - } - - err = viper.ReadInConfig() - if err != nil { - log.Fatal(err) - } - } - - host = viper.GetString("core.host") - user = viper.GetString("core.user") - token = viper.GetString("core.token") - tlsSkipVerify := viper.GetBool("tls.skip_verify") - - if host != "" && user != "" && token != "" { - return host, user, token, tlsSkipVerify - } - - user = getUser(host, token, tlsSkipVerify) - if strings.TrimSpace(os.Getenv("LAB_CORE_TOKEN")) == "" && strings.TrimSpace(os.Getenv("LAB_CORE_HOST")) == "" { - viper.Set("core.user", user) - viper.WriteConfig() - } - - return host, user, token, tlsSkipVerify -} - func loadTLSCerts() string { c := viper.AllSettings() @@ -126,28 +41,12 @@ func loadTLSCerts() string { return tls["ca_file"].(string) } -func getUser(host, token string, skipVerify bool) string { - httpClient := &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: skipVerify, - }, - }, - } - lab, _ := gitlab.NewClient(token, gitlab.WithHTTPClient(httpClient), gitlab.WithBaseURL(host+"/api/v4")) - u, _, err := lab.Users.CurrentUser() - if err != nil { - log.Fatal(err) - } - return u.Username -} - func main() { log.SetFlags(log.LstdFlags | log.Lshortfile) cmd.Version = version if !skipInit() { ca := loadTLSCerts() - h, u, t, skipVerify := loadConfig() + h, u, t, skipVerify := config.LoadConfig() if ca != "" { lab.InitWithCustomCA(h, u, t, ca)