Skip to content

Commit

Permalink
Merge pull request #417 from prarit/mr_show_config_comment
Browse files Browse the repository at this point in the history
mr show config comment

Add a local "comments" config option that always sets "--comments" for the mr and issue show commands.
  • Loading branch information
prarit authored Sep 6, 2020
2 parents 084623b + 4104b40 commit 146904c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 46 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ instance. There are several ways to provide this information to `lab`:
2. environment variables: `CI_PROJECT_URL`, `CI_JOB_TOKEN`;
- Note: these are meant for when `lab` is running within a GitLab CI pipeline
3. directory-specific configuration file in [Tom's Obvious, Minimal Language (TOML)](https://github.com/toml-lang/toml): `./lab.toml`;
4. user-specific configuration file in HCL: `~/.config/lab/lab.toml`.
4. user-specific configuration file in TOML: `~/.config/lab/lab.toml`.

These are checked in order. If no suitable config values are found, `lab` will
prompt for your GitLab information and save it into `~/.config/lab/lab.toml`.
Expand All @@ -99,6 +99,13 @@ For example:
$ lab
Enter default GitLab host (default: https://gitlab.com):
Enter default GitLab token:
```

Additionally, there are command-specific configuration files in TOML: .git/lab/[command]-metadata.toml which provide users the ability to override some command line options:

```
comments = true # sets --comments on 'mr show' commands
```
# Completions

Expand Down
46 changes: 25 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 @@ -44,6 +65,9 @@ var issueShowCmd = &cobra.Command{
printIssue(issue, rn, renderMarkdown)

showComments, _ := cmd.Flags().GetBool("comments")
if showComments == false {
showComments = viper.GetBool("comments")
}
if showComments {
discussions, err := lab.IssueListDiscussions(rn, int(issueNum))
if err != nil {
Expand All @@ -57,6 +81,7 @@ var issueShowCmd = &cobra.Command{

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

Expand Down Expand Up @@ -123,27 +148,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
52 changes: 28 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 @@ -68,6 +92,9 @@ var mrShowCmd = &cobra.Command{
}

showComments, _ := cmd.Flags().GetBool("comments")
if showComments == false {
showComments = viper.GetBool("comments")
}
if showComments {
discussions, err := lab.MRListDiscussions(rn, int(mrNum))
if err != nil {
Expand All @@ -81,6 +108,7 @@ var mrShowCmd = &cobra.Command{

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

Expand Down Expand Up @@ -164,30 +192,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 146904c

Please sign in to comment.