template / commit
Added config type to the app, using this for creating a config file or reading a created one.
.vscode/launch.json | 18 ++++++++++++++ README.md | 17 ++++++++++++- config.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 +++- go.sum | 4 +++ main.go | 47 ++++++++++++++++++++++++++++++---- utils.go | 17 +++++++++++++ 7 files changed, 173 insertions(+), 7 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..896dd01 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": ".", + "args": [ + "blog" + ] + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 58321a7..63bbce6 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,32 @@ template is a cli that creates templated files and folders. +## Dev loop + +- `go run .` - shows `profile: default` +- `go run . work` - shows `profile: work` +- `go run . --profile work` - shows `profile: work` +- `go run . -p work` - shows `profile: work` + ## Installation `go install` -## Running +### Running - `template` - shows `profile: default` - `template work` - shows `profile: work` - `template --profile work` - shows `profile: work` - `template -p work` - shows `profile: work` +### Install dev loop + +Here is a way to install the app, clear out the local config, and run the app: + +``` +go install; rm ~\.template.yaml -ErrorAction SilentlyContinue ; template +``` + ## Old stuff below The old-but-possibly-relevant README is below. diff --git a/config.go b/config.go new file mode 100644 index 0000000..115748e --- /dev/null +++ b/config.go @@ -0,0 +1,72 @@ +package main + +import ( + "os" + + "gopkg.in/yaml.v3" +) + +type ProfileType string + +const ( + FileType ProfileType = "file" + FolderType = "folder" +) + +type Profile struct { + Path string + Type ProfileType +} + +type Config struct { + Profiles map[string]Profile +} + +func defaultConfig() Config { + return Config{ + Profiles: map[string]Profile{ + "default": { + Path: "C:/DailyNotes", + Type: FileType, + }, + "blog": { + Path: "~/Dropbox/Blog", + Type: FileType, + }, + "work": { + Path: "C:/work", + Type: FolderType, + }, + }, + } +} + +func loadConfig(path string) (Config, error) { + file, error := os.ReadFile(path) + if error != nil { + return Config{}, error + } + + var config Config + error = yaml.Unmarshal(file, &config) + if error != nil { + return Config{}, error + } + + return config, nil +} + +func saveConfig(path string, config Config) error { + file, error := os.Create(path) + if error != nil { + return error + } + defer file.Close() + encoder := yaml.NewEncoder(file) + error = encoder.Encode(config) + if error != nil { + return error + } + + return nil +} diff --git a/go.mod b/go.mod index 8104605..fb6c015 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module gitlab.com/fuzzy-rp/template go 1.19 -require github.com/urfave/cli/v2 v2.16.3 +require ( + github.com/urfave/cli/v2 v2.16.3 + gopkg.in/yaml.v3 v3.0.1 +) require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect diff --git a/go.sum b/go.sum index e782ae9..6fad046 100644 --- a/go.sum +++ b/go.sum @@ -6,3 +6,7 @@ github.com/urfave/cli/v2 v2.16.3 h1:gHoFIwpPjoyIMbJp/VFd+/vuD0dAgFK4B6DpEMFJfQk= github.com/urfave/cli/v2 v2.16.3/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 2a7caf2..c76a9bd 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,14 @@ import ( "fmt" "log" "os" + "path/filepath" "github.com/urfave/cli/v2" // imports as package "cli" ) func main() { defaultProfileName := "default" - var profile string + var profileName string app := &cli.App{ Name: "template", @@ -22,17 +23,53 @@ func main() { Aliases: []string{"p"}, Value: defaultProfileName, Usage: "`PROFILE` to use for the template", - Destination: &profile, + Destination: &profileName, // Required: true, // EnvVars: []string{"TEMPLATE_PROFILE"}, }, }, Action: func(context *cli.Context) error { - if profile == defaultProfileName && context.Args().Len() > 0 { - profile = context.Args().Get(0) + if profileName == defaultProfileName && context.Args().Len() > 0 { + profileName = context.Args().Get(0) } - message := fmt.Sprintf("profile: %v", profile) + message := fmt.Sprintf("Profile: %v", profileName) fmt.Println(message) + + userProfile, err := os.UserHomeDir() + if err != nil { + processError(err, "Getting user home dir") + } + + configFilePath := filepath.Join(userProfile, ".template.yaml") + config, err := loadConfig(configFilePath) + if err != nil { + fmt.Printf("No config present at '%v', creating and using sample config", configFilePath) + fmt.Println() + + // create config and save + err := saveConfig(configFilePath, defaultConfig()) + if err != nil { + processError(err, "Error while saving") + } + + // load config + config, err = loadConfig(configFilePath) + if err != nil { + processError(err, "Error while loading") + } + } else { + fmt.Printf("Config loaded from '%v'", configFilePath) + fmt.Println() + } + + profile, ok := config.Profiles[profileName] + if ok { + fmt.Println("Found profile!") + fmt.Println(profile) + } else { + processError(fmt.Errorf("Unable to find profile '%v'", profileName), "Error finding profile") + } + return nil }, } diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..d76a032 --- /dev/null +++ b/utils.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "os" +) + +func processError(err error, message string) { + fmt.Println() + fmt.Println("---") + + fmt.Println("Fatal error encountered.") + fmt.Println("Message: " + message) + fmt.Println(err) + + os.Exit(2) +}