Skip to content

Commit

Permalink
feat: added api command and edited config.go to be more extensible an…
Browse files Browse the repository at this point in the history
…d usable by other files
  • Loading branch information
Slug-Boi committed Jul 9, 2024
1 parent 96d38cd commit 727eeae
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 22 deletions.
44 changes: 44 additions & 0 deletions src/cmd/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"os"

"github.com/Slug-Boi/aion-cli/forms"
"github.com/spf13/cobra"
)

// apiCmd represents the api command
var apiCmd = &cobra.Command{
Use: "api <API_Key>",
Short: "This sub command edits the API key in the config file.",
Long: `This command allows you to edit the API key in the config file.
The config file is located in the user's config directory. Example: ` + UserConf() + `config.json`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Reading current config file")

conf, err := forms.GetConfigFile()
if err != nil {
fmt.Println(err)
}

conf.Apikey = args[0]

f, err := os.OpenFile(UserConf()+"config.json", os.O_RDWR, 0644)
if err != nil {
fmt.Println(err)
}

WriteConfig(f, conf)
fmt.Println("API key updated")

},
}

func init() {
configCmd.AddCommand(apiCmd)
}
56 changes: 34 additions & 22 deletions src/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

"github.com/Slug-Boi/aion-cli/forms"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +17,9 @@ var configCmd = &cobra.Command{
`,
Run: func(cmd *cobra.Command, args []string) {
CheckConfig()

fmt.Println("Config file exists")
fmt.Println("Please use sub commands to modify the config file. A list of these can be found by using -h or --help.")
},
}

Expand All @@ -24,7 +28,7 @@ func init() {

}

func userConf() string {
func UserConf() string {
userConfig, err := os.UserConfigDir()
if err != nil {
fmt.Println("Error getting user config directory exiting")
Expand All @@ -38,7 +42,7 @@ func CheckConfig() {

// Check if config file exists
// If it does not exist, call creator function
userConfig := userConf()
userConfig := UserConf()

_, err := os.Stat(userConfig + "config.json")
if os.IsNotExist(err) {
Expand All @@ -56,47 +60,55 @@ func CreateConfig() {
//TODO: Probably move this to a seperate function
fmt.Println("Creating config file")
// Create config file
userConfig := userConf()
userConfig := UserConf()

// Create config directory
err := os.MkdirAll(userConfig, 0755)
if err != nil {
fmt.Println("Error creating config directory at: %s \nExiting", userConfig)
fmt.Printf("Error creating config directory at: %s \nExiting", userConfig)
os.Exit(1)
}

// Create and open config file
f, err := os.OpenFile(userConfig+"config.json", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)

if err != nil {
fmt.Println("Error creating config file at: %s \nExiting", userConfig)
fmt.Printf("Error creating config file at: %s \nExiting", userConfig)
os.Exit(1)
}

// Write to config file
writer := bufio.NewWriter(f)

if err != nil {
fmt.Println("Error creating config file at: %s \nExiting", userConfig)
os.Exit(1)
}

writer.WriteString(`{`)

// Write API key for strawpoll to config file
// Ask for user input for API key
fmt.Println("Enter the API key for your strawpoll account: \n(it can be found here https://strawpoll.com/account/settings/ under the API section)")
fmt.Scanln(&response)
fmt.Println("Writing API key to config file...")
writer.WriteString(`"spAPI": "` + response + `"`)

// Write form ID field to config file (empty for now)
fmt.Println("Writing form ID field to config file...")
writer.WriteString(`"formID": ""`)
// Call writer to write to config file
WriteConfig(f, forms.Config{Apikey: response})

writer.WriteString(`}`)
os.Exit(0)

} else {
fmt.Println("Exiting")
os.Exit(0)
}
}

func WriteConfig(f *os.File, conf forms.Config) {

// Write to config file
writer := bufio.NewWriter(f)

writer.WriteString("{\n")

// Write API key for strawpoll to config file
fmt.Println("Writing API key to config file...")
writer.WriteString(fmt.Sprintf("\t\"spAPI\": \"%s\"\n", conf.Apikey))

// Write form ID field to config file (empty for now)
fmt.Println("Writing form ID field to config file...")
writer.WriteString(fmt.Sprintf("\t\"formID\": \"%s\"", conf.FormID))

writer.WriteString("\n}")

writer.Flush()

}

0 comments on commit 727eeae

Please sign in to comment.