diff --git a/cmd/issue_show.go b/cmd/issue_show.go index cb16e2dd..83d5fbd9 100644 --- a/cmd/issue_show.go +++ b/cmd/issue_show.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "log" - "os" "strings" "time" @@ -15,6 +14,7 @@ import ( "github.com/spf13/viper" gitlab "github.com/xanzy/go-gitlab" "github.com/zaquestion/lab/internal/action" + "github.com/zaquestion/lab/internal/config" lab "github.com/zaquestion/lab/internal/gitlab" ) @@ -35,26 +35,7 @@ var issueShowCmd = &cobra.Command{ log.Fatal(err) } - viper.Reset() - viper.AddConfigPath(".git/lab") - viper.SetConfigName("show_metadata") - viper.SetConfigType("toml") - // write data - if _, ok := viper.ReadInConfig().(viper.ConfigFileNotFoundError); ok { - if _, err := os.Stat(".git/lab"); os.IsNotExist(err) { - os.MkdirAll(".git/lab", os.ModePerm) - } - if err := viper.WriteConfigAs(metadatafile); err != nil { - log.Fatal(err) - } - if err := viper.ReadInConfig(); err != nil { - log.Fatal(err) - } - } else { - if err := viper.ReadInConfig(); err != nil { - log.Fatal(err) - } - } + config.LoadWorkTreeConfig("show") noMarkdown, _ := cmd.Flags().GetBool("no-markdown") if err != nil { @@ -81,7 +62,7 @@ var issueShowCmd = &cobra.Command{ printDiscussions(discussions, since, int(issueNum)) } - viper.Reset() + config.FinishWorkTreeConfig() }, } @@ -145,9 +126,6 @@ WebURL: %s func printDiscussions(discussions []*gitlab.Discussion, since string, issueNum int) { NewAccessTime := time.Now().UTC() - // default path for metadata config file - metadatafile := ".git/lab/show_metadata.toml" - issueEntry := fmt.Sprintf("issue%d", issueNum) // if specified on command line use that, o/w use config, o/w Now var ( @@ -209,7 +187,7 @@ func printDiscussions(discussions []*gitlab.Discussion, since string, issueNum i if sinceIsSet == false { viper.Set(issueEntry, NewAccessTime) - viper.WriteConfigAs(metadatafile) + config.WriteWorkTreeConfig("show") } } diff --git a/cmd/mr_show.go b/cmd/mr_show.go index 61e014fd..dc4c177b 100644 --- a/cmd/mr_show.go +++ b/cmd/mr_show.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "log" - "os" "strings" "time" @@ -15,6 +14,7 @@ import ( "github.com/spf13/viper" gitlab "github.com/xanzy/go-gitlab" "github.com/zaquestion/lab/internal/action" + "github.com/zaquestion/lab/internal/config" "github.com/zaquestion/lab/internal/git" lab "github.com/zaquestion/lab/internal/gitlab" ) @@ -24,9 +24,6 @@ var ( mrShowPatchReverse bool ) -// default path for metadata config file -var metadatafile = ".git/lab/show_metadata.toml" - var mrShowCmd = &cobra.Command{ Use: "show [remote] ", Aliases: []string{"get"}, @@ -44,26 +41,7 @@ var mrShowCmd = &cobra.Command{ log.Fatal(err) } - viper.Reset() - viper.AddConfigPath(".git/lab") - viper.SetConfigName("show_metadata") - viper.SetConfigType("toml") - // write data - if _, ok := viper.ReadInConfig().(viper.ConfigFileNotFoundError); ok { - if _, err := os.Stat(".git/lab"); os.IsNotExist(err) { - os.MkdirAll(".git/lab", os.ModePerm) - } - if err := viper.WriteConfigAs(metadatafile); err != nil { - log.Fatal(err) - } - if err := viper.ReadInConfig(); err != nil { - log.Fatal(err) - } - } else { - if err := viper.ReadInConfig(); err != nil { - log.Fatal(err) - } - } + config.LoadWorkTreeConfig("show") noMarkdown, _ := cmd.Flags().GetBool("no-markdown") if err != nil { @@ -108,7 +86,7 @@ var mrShowCmd = &cobra.Command{ printMRDiscussions(discussions, since, int(mrNum)) } - viper.Reset() + config.FinishWorkTreeConfig() }, } @@ -254,7 +232,7 @@ func printMRDiscussions(discussions []*gitlab.Discussion, since string, mrNum in if sinceIsSet == false { viper.Set(mrEntry, NewAccessTime) - viper.WriteConfigAs(metadatafile) + config.WriteWorkTreeConfig("show") } } diff --git a/internal/config/config.go b/internal/config/config.go index 0263d232..903824a1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -281,3 +281,41 @@ func LoadConfig() (string, string, string, string, bool) { return host, user, token, ca_file, tlsSkipVerify } + +// default path for work tree config file +var worktreepath = ".git/lab/" + +// LoadWorkTreeConfig opens and reads the .git/lab/[cmd]_string.toml +// metadata file +func LoadWorkTreeConfig(cmd string) { + viper.Reset() + viper.AddConfigPath(worktreepath) + viper.SetConfigName(cmd + "_metadata") + viper.SetConfigType("toml") + + if _, ok := viper.ReadInConfig().(viper.ConfigFileNotFoundError); ok { + if _, err := os.Stat(worktreepath); os.IsNotExist(err) { + os.MkdirAll(worktreepath, os.ModePerm) + } + if err := viper.WriteConfigAs(worktreepath + cmd + "_metadata.toml"); err != nil { + log.Fatal(err) + } + if err := viper.ReadInConfig(); err != nil { + log.Fatal(err) + } + } else { + if err := viper.ReadInConfig(); err != nil { + log.Fatal(err) + } + } +} + +// WriteWorkTreeConfig saves the .git/lab/[cmd]_string.toml metadata file +func WriteWorkTreeConfig(cmd string) { + viper.WriteConfigAs(worktreepath + cmd + "_metadata.toml") +} + +// FinishWorkTreeConfig closes the .git/lab/[cmd]_string.toml metadata file +func FinishWorkTreeConfig() { + viper.Reset() +}