Skip to content

Commit

Permalink
config: update auth/login and clone use the globalPath and the valida…
Browse files Browse the repository at this point in the history
…tor (#18)
  • Loading branch information
craftamap committed Sep 8, 2021
1 parent b959590 commit 3682ddc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 27 deletions.
37 changes: 28 additions & 9 deletions cmd/commands/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package login

import (
"fmt"
"path/filepath"

"github.com/craftamap/bb/config"
"github.com/craftamap/bb/util/logging"

"github.com/AlecAivazis/survey/v2"
Expand All @@ -15,11 +17,19 @@ import (
func Add(authCmd *cobra.Command, globalOpts *options.GlobalOptions) {
loginCmd := &cobra.Command{
Use: "login",
Run: func(cmd *cobra.Command, args []string) {
oldPw := viper.GetString("password")
Run: func(_ *cobra.Command, _ []string) {
configDirectory, filename := config.GetGlobalConfigurationPath()
path := filepath.Join(configDirectory, filename)
// TODO: extract tmpVp stuff to a seperate file
tmpVp := viper.New()
tmpVp.SetConfigType("toml")
tmpVp.SetConfigFile(path)
tmpVp.ReadInConfig()

oldPw := tmpVp.GetString("password")

if oldPw != "" {
fmt.Println(aurora.Yellow("::"), aurora.Bold("Warning:"), "You are already logged in as", viper.GetString("username"))
logging.Warning("You are already logged in as ", tmpVp.GetString("username"))
cont := false
err := survey.AskOne(&survey.Confirm{Message: "Do you want to overwrite this?"}, &cont)
if err != nil {
Expand Down Expand Up @@ -60,18 +70,27 @@ func Add(authCmd *cobra.Command, globalOpts *options.GlobalOptions) {
logging.Error(err)
return
}
username, err := config.BbConfigurationValidation.ValidateEntry("username", answers.Username)
if err != nil {
logging.Error(err)
return
}
password, err := config.BbConfigurationValidation.ValidateEntry("password", answers.Password)
if err != nil {
logging.Error(err)
return
}

tmpVp.Set("username", username)
tmpVp.Set("password", password)

viper.Set("username", answers.Username)
viper.Set("password", answers.Password)

// TODO: fix
err = viper.WriteConfig()
err = tmpVp.WriteConfig()
if err != nil {
logging.Error(err)
return
}

logging.Success(fmt.Sprint("Stored credentials successfully to", viper.ConfigFileUsed()))
logging.Success(fmt.Sprint("Stored credentials successfully to", tmpVp.ConfigFileUsed()))
},
}

Expand Down
17 changes: 2 additions & 15 deletions cmd/commands/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (

"github.com/craftamap/bb/cmd/options"
"github.com/craftamap/bb/config"
bbgit "github.com/craftamap/bb/git"
"github.com/craftamap/bb/util/logging"
"github.com/kirsle/configdir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -52,13 +50,13 @@ func Add(rootCmd *cobra.Command, _ *options.GlobalOptions) {
var filename string
if Local {
var err error
configDirectory, filename, err = GetLocalConfigurationPath()
configDirectory, filename, err = config.GetLocalConfigurationPath()
if err != nil {
logging.Error(err)
return
}
} else {
configDirectory, filename = GetGlobalConfigurationPath()
configDirectory, filename = config.GetGlobalConfigurationPath()
}

// If the directory does not exist, something is off:
Expand Down Expand Up @@ -165,14 +163,3 @@ func copyFileContent(src string, dst string) error {
_, err = io.Copy(destination, source)
return err
}

// TODO: Extract to util
func GetGlobalConfigurationPath() (configDirectory string, filename string) {
configDirectory = configdir.LocalConfig("bb")
return configDirectory, "configuration.toml"
}

func GetLocalConfigurationPath() (configDirectory, filename string, err error) {
configDirectory, err = bbgit.RepoPath()
return configDirectory, ".bb", err
}
23 changes: 20 additions & 3 deletions cmd/commands/repo/clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package clone

import (
"fmt"
"path/filepath"
"strings"

"github.com/craftamap/bb/config"
"github.com/craftamap/bb/util/logging"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -34,9 +36,24 @@ func Add(repoCmd *cobra.Command, globalOpts *options.GlobalOptions) {
logging.Error(err)
return
}
viper.Set("git_protocol", gitProtocol)
// TODO: fix
viper.WriteConfig()

configDirectory, filename := config.GetGlobalConfigurationPath()
path := filepath.Join(configDirectory, filename)
// TODO: extract tmpVp stuff to a seperate file
tmpVp := viper.New()
tmpVp.SetConfigType("toml")
tmpVp.SetConfigFile(path)
tmpVp.ReadInConfig()

gitProtocolI, err := config.BbConfigurationValidation.ValidateEntry("git_protocol", gitProtocol)
if err != nil {
logging.Error(err)
return
}
gitProtocol = gitProtocolI.(string)

tmpVp.Set("git_protocol", gitProtocol)
tmpVp.WriteConfig()
}

if len(args) == 0 {
Expand Down
17 changes: 17 additions & 0 deletions config/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config

import (
bbgit "github.com/craftamap/bb/git"
"github.com/kirsle/configdir"
)

// TODO: Extract to util
func GetGlobalConfigurationPath() (configDirectory string, filename string) {
configDirectory = configdir.LocalConfig("bb")
return configDirectory, "configuration.toml"
}

func GetLocalConfigurationPath() (configDirectory, filename string, err error) {
configDirectory, err = bbgit.RepoPath()
return configDirectory, ".bb", err
}

0 comments on commit 3682ddc

Please sign in to comment.