Skip to content

Commit

Permalink
show: Reorganize config code
Browse files Browse the repository at this point in the history
Call the config setup code from the main Run function.  This allows
config variables to be used for all functions.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Sep 6, 2020
1 parent 084623b commit dbf3165
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
43 changes: 22 additions & 21 deletions cmd/issue_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ 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)
}
}

noMarkdown, _ := cmd.Flags().GetBool("no-markdown")
if err != nil {
log.Fatal(err)
Expand All @@ -57,6 +78,7 @@ var issueShowCmd = &cobra.Command{

printDiscussions(discussions, since, int(issueNum))
}
viper.Reset()
},
}

Expand Down Expand Up @@ -123,27 +145,6 @@ func printDiscussions(discussions []*gitlab.Discussion, since string, issueNum i
// default path for metadata config file
metadatafile := ".git/lab/show_metadata.toml"

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)
}
}

issueEntry := fmt.Sprintf("issue%d", issueNum)
// if specified on command line use that, o/w use config, o/w Now
var (
Expand Down
49 changes: 25 additions & 24 deletions cmd/mr_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var (
mrShowPatchReverse bool
)

// default path for metadata config file
var metadatafile = ".git/lab/show_metadata.toml"

var mrShowCmd = &cobra.Command{
Use: "show [remote] <id>",
Aliases: []string{"get"},
Expand All @@ -41,6 +44,27 @@ 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)
}
}

noMarkdown, _ := cmd.Flags().GetBool("no-markdown")
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -81,6 +105,7 @@ var mrShowCmd = &cobra.Command{

printMRDiscussions(discussions, since, int(mrNum))
}
viper.Reset()
},
}

Expand Down Expand Up @@ -164,30 +189,6 @@ WebURL: %s
func printMRDiscussions(discussions []*gitlab.Discussion, since string, mrNum int) {
NewAccessTime := time.Now().UTC()

// default path for metadata config file
metadatafile := ".git/lab/show_metadata.toml"

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)
}
}

mrEntry := fmt.Sprintf("mr%d", mrNum)
// if specified on command line use that, o/w use config, o/w Now
var (
Expand Down

0 comments on commit dbf3165

Please sign in to comment.