Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cobra #10

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

type Resource string

const Name string = "weather"

const (
SiteList Resource = "sitelist"
RegionList Resource = "regionlist"
Expand Down
188 changes: 0 additions & 188 deletions cmd.go

This file was deleted.

18 changes: 16 additions & 2 deletions cmd/weather/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package main

import "github.com/rwx-yxu/weather"
import (
"os"

"github.com/rwx-yxu/weather/conf"
"github.com/rwx-yxu/weather/site"
"github.com/spf13/cobra"
)

func main() {
weather.Cmd.Run()
var rootCmd = &cobra.Command{
Use: "weather",
Short: "Weather cli",
}
rootCmd.AddCommand(site.BaseCmd)
rootCmd.AddCommand(conf.BaseCmd)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
54 changes: 54 additions & 0 deletions conf/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package conf

import (
"errors"
"fmt"
"log"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func init() {
SoftInit()
err := Load()
if err != nil {
log.Fatalf("Unable to load conf file %s", err)
}
BaseCmd.AddCommand(initCmd)
BaseCmd.AddCommand(dataCmd)
}

// sayCmd represents the say command
var BaseCmd = &cobra.Command{
Use: "conf",
Short: "top level conf command to initialise the .env configuration file for the weather application",
}

var initCmd = &cobra.Command{
Use: "init",
Short: "Reinitialise the configuration .env file in the user cache directory",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Attempting to create env file...")
if err := Init(); err != nil && !errors.Is(err, ErrorConfigFileExists) {
return err
}
fmt.Printf(".env initialised at: %s\n", filePath())
return nil
},
}

var dataCmd = &cobra.Command{
Use: "data",
Short: fmt.Sprintf("Prints out all of the stored env variables at: %s", filePath()),
RunE: func(cmd *cobra.Command, args []string) error {
keys, err := keys()
if err != nil {
return err
}
for _, key := range keys {
fmt.Printf("%s=%s\n", key, viper.Get(key))
}
return nil
},
}
82 changes: 82 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package conf

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/rwx-yxu/weather/app"
"github.com/rwxrob/fs/file"
"github.com/spf13/viper"
)

var ErrorCannotResolvePath = errors.New("cannot resolve path")
var ErrorConfigFileExists = errors.New("config file already exists")

func Load() error {
d := dirPath()
if d == "" {
return errors.New("invalid dir path")
}
viper.AddConfigPath(d)
viper.SetConfigName(app.Name)
viper.SetConfigType("env")

if err := viper.ReadInConfig(); err != nil {
fmt.Errorf("error reading config file, %s", err)
}
return nil
}

func exist(path string) bool { return file.Exists(path) }

func dirPath() string {
userCacheDir, err := os.UserCacheDir()
if err != nil {
return ""
}
return filepath.Join(userCacheDir, app.Name)
}

func filePath() string {
name := fmt.Sprintf("%s.env", app.Name)
return filepath.Join(dirPath(), name)
}

func Init() error {
d := dirPath()
if d == "" {
return fmt.Errorf("%w for %s", ErrorCannotResolvePath, d)
}

f, err := os.Create(filePath())
if err != nil {
return fmt.Errorf("unable to initialise create file: %w", err)
}

defer f.Close()

return nil
}

func SoftInit() error {
if _, err := os.Stat(filePath()); err == nil {
return fmt.Errorf("%w at: %s", ErrorConfigFileExists, filePath)
}

err := Init()
if err != nil {
return err
}
return nil
}

func keys() ([]string, error) {
err := Load()
if err != nil {
return nil, err
}

return viper.AllKeys(), nil
}
Loading