Skip to content

Commit

Permalink
config: Move config init code
Browse files Browse the repository at this point in the history
Move the config initialization code from main.go to
internal/config/config.go.

Suggested-by: Bruno Meneguele <bmeneg@redhat.com>
Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Sep 4, 2020
1 parent 2c6e0d8 commit 8dda6ff
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 102 deletions.
101 changes: 101 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
103 changes: 1 addition & 102 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8dda6ff

Please sign in to comment.