Skip to content

Commit

Permalink
Add global config
Browse files Browse the repository at this point in the history
  • Loading branch information
ScopeSV committed Aug 24, 2022
1 parent 665e2d2 commit 18e43f8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .config.toml
@@ -0,0 +1,7 @@
[jira]
apikey = '3'
space = '1'

[user]
email = '2'

1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
config.toml
gojira.toml
Empty file removed .nvimlog
Empty file.
12 changes: 7 additions & 5 deletions main.go
Expand Up @@ -11,17 +11,19 @@ import (
"github.com/spf13/viper"
)

var filename string = "config.toml"
const FILE_NAME string = "gojira.toml"

const PATH string = "."

func init() {
viper.SetConfigName(filename)
viper.SetConfigName(FILE_NAME)
viper.SetConfigType("toml")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.config/gojira")

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("It looks like you've never ran the setup before")
setup.RunBasicSetup(filename, bufio.NewReader(os.Stdin))
setup.RunBasicSetup(FILE_NAME, bufio.NewReader(os.Stdin))
} else {
log.Fatalf("Error reading config file, %s", err)
}
Expand All @@ -30,7 +32,7 @@ func init() {
}

func main() {
app := cmd.CreateCliApp(filename)
app := cmd.CreateCliApp(PATH)

if err := app.Run(os.Args); err != nil {
log.Fatalf("Something went wrong, %v", err)
Expand Down
7 changes: 6 additions & 1 deletion setup/basicSetup.go
Expand Up @@ -2,6 +2,7 @@ package setup

import (
"fmt"
"log"
"os"

"github.com/spf13/viper"
Expand All @@ -22,7 +23,11 @@ func RunBasicSetup(filename string, r InputReader) {
r,
)

viper.WriteConfigAs(filename)
if err := viper.WriteConfigAs(filename); err != nil {
log.Fatalf("Could not write config: %v", err)
}

SaveConfigGlobally(filename)

if IsBasicSetupComplete() {
fmt.Println("Basic Setup complete")
Expand Down
25 changes: 25 additions & 0 deletions setup/globalSave.go
@@ -0,0 +1,25 @@
package setup

import (
"log"
"os"
)

func SaveConfigGlobally(filename string) {

dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

file, err := os.ReadFile(filename)

if err != nil {
log.Fatal(err)
}

os.MkdirAll(dirname+"/.config/gojira", 0700)
if err := os.WriteFile(dirname+"/.config/gojira/"+filename, file, 0644); err != nil {
log.Fatalf("Could not write config: %v", err)
}
}
2 changes: 2 additions & 0 deletions setup/transitionSetup.go
Expand Up @@ -32,6 +32,8 @@ func RunTransitionSetup(filename string, r InputReader) {
log.Fatalf("Something went wrong when writing config file: %v", err)
}

SaveConfigGlobally(filename)

fmt.Println("Transition Setup complete")
fmt.Println("You may at any point run `gojira setup` to change your settings")
}

0 comments on commit 18e43f8

Please sign in to comment.