|
| 1 | +/* |
| 2 | +Copyright © 2025 sottey |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in |
| 12 | +all copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +THE SOFTWARE. |
| 21 | +*/ |
| 22 | +package cmd |
| 23 | + |
| 24 | +import ( |
| 25 | + "fmt" |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + "strings" |
| 29 | + |
| 30 | + "github.com/spf13/cobra" |
| 31 | + "github.com/spf13/viper" |
| 32 | +) |
| 33 | + |
| 34 | +var BaseDir string |
| 35 | + |
| 36 | +var rootCmd = &cobra.Command{ |
| 37 | + Use: "goteplan", |
| 38 | + Short: "A CLI for NotePlan notes management", |
| 39 | +} |
| 40 | + |
| 41 | +func Execute() { |
| 42 | + err := rootCmd.Execute() |
| 43 | + if err != nil { |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func init() { |
| 49 | + dataPath, _ := expandPath("~/Library/Containers/co.noteplan.NotePlan-setapp/Data/Library/Application Support/co.noteplan.NotePlan-setapp") |
| 50 | + configPath, _ := expandPath("~/") |
| 51 | + rootCmd.PersistentFlags().StringVarP(&BaseDir, "basedir", "b", "", "Root location of the NotePlan data") |
| 52 | + viper.BindPFlag("basedir", rootCmd.PersistentFlags().Lookup("basedir")) |
| 53 | + |
| 54 | + viper.SetConfigName(".goteplan") |
| 55 | + viper.SetConfigType("json") |
| 56 | + viper.AddConfigPath(configPath) |
| 57 | + if err := viper.ReadInConfig(); err != nil { |
| 58 | + if _, ok := err.(viper.ConfigFileNotFoundError); ok { |
| 59 | + fmt.Println("Config file not found. Creating...") |
| 60 | + viper.SetDefault("basedir", dataPath) |
| 61 | + if errTwo := viper.SafeWriteConfig(); errTwo != nil { |
| 62 | + fmt.Printf("Error creating config file: '%v'\n", errTwo) |
| 63 | + } |
| 64 | + } else { |
| 65 | + fmt.Printf("Error opening config file: %v\n", err) |
| 66 | + return |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + BaseDir = viper.GetString("basedir") |
| 71 | +} |
| 72 | + |
| 73 | +func expandPath(path string) (string, error) { |
| 74 | + if strings.HasPrefix(path, "~") { |
| 75 | + homeDir, err := os.UserHomeDir() |
| 76 | + if err != nil { |
| 77 | + return "", err |
| 78 | + } |
| 79 | + return filepath.Join(homeDir, path[1:]), nil |
| 80 | + } |
| 81 | + return filepath.Abs(path) |
| 82 | +} |
0 commit comments