Skip to content

Commit

Permalink
config: Add WorkTree functions
Browse files Browse the repository at this point in the history
Move and generalize the show commands' git work tree configfile code to
the config code.  The move allows any command to initialize and use a
work tree config file.

Suggested by: Zaq? Wiedmann <zaquestion@gmail.com>
Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Sep 12, 2020
1 parent d2a7df4 commit 4dc20f9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 52 deletions.
30 changes: 4 additions & 26 deletions cmd/issue_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"log"
"os"
"strings"
"time"

Expand All @@ -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"
)

Expand All @@ -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 {
Expand All @@ -81,7 +62,7 @@ var issueShowCmd = &cobra.Command{

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

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

Expand Down
30 changes: 4 additions & 26 deletions cmd/mr_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"log"
"os"
"strings"
"time"

Expand All @@ -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"
)
Expand All @@ -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] <id>",
Aliases: []string{"get"},
Expand All @@ -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 {
Expand Down Expand Up @@ -108,7 +86,7 @@ var mrShowCmd = &cobra.Command{

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

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

Expand Down
38 changes: 38 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 4dc20f9

Please sign in to comment.