File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package lib
22
33import (
4+ "fmt"
45 "path/filepath"
56
67 sys "github.com/8bitalex/raid/src/internal/sys"
@@ -21,21 +22,27 @@ var CfgPath string
2122var defaultConfigPath = filepath .Join (sys .GetHomeDir (), ConfigDirName )
2223
2324func InitConfig () error {
24- viper .SetConfigFile (getOrCreateConfigFile ())
25+ path , err := getOrCreateConfigFile ()
26+ if err != nil {
27+ return err
28+ }
29+ viper .SetConfigFile (path )
2530 if err := viper .ReadInConfig (); err != nil {
2631 return err
2732 }
2833 return nil
2934}
3035
31- func getOrCreateConfigFile () string {
36+ func getOrCreateConfigFile () ( string , error ) {
3237 path := getPath ()
3338 if ! sys .FileExists (path ) {
34- if f , err := sys .CreateFile (path ); err == nil {
35- f .Close ()
39+ f , err := sys .CreateFile (path )
40+ if err != nil {
41+ return "" , fmt .Errorf ("failed to create config file at %s: %w" , path , err )
3642 }
43+ f .Close ()
3744 }
38- return path
45+ return path , nil
3946}
4047
4148func getPath () string {
Original file line number Diff line number Diff line change @@ -98,10 +98,11 @@ func isGitInstalled() bool {
9898}
9999
100100func clone (path string , url string , branch string ) error {
101- args := []string {"clone" , url , path }
101+ args := []string {"clone" }
102102 if branch != "" {
103103 args = append (args , "--branch" , branch )
104104 }
105+ args = append (args , url , path )
105106 cmd := exec .Command ("git" , args ... )
106107 cmd .Stdout = os .Stdout
107108 cmd .Stderr = os .Stderr
Original file line number Diff line number Diff line change @@ -54,17 +54,10 @@ func FileExists(path string) bool {
5454 return os .IsPermission (err )
5555}
5656
57- // Expand expands environment variables and home directory references in each whitespace-delimited
58- // token of input, then rejoins them with spaces .
57+ // Expand expands environment variables in input without tokenizing, preserving
58+ // quoting and spacing. Use ExpandPath for file system paths that also need ~ expansion .
5959func Expand (input string ) string {
60- if input == "" {
61- return input
62- }
63- parts := SplitInput (input )
64- for i , p := range parts {
65- parts [i ] = ExpandPath (p )
66- }
67- return strings .Join (parts , " " )
60+ return os .ExpandEnv (input )
6861}
6962
7063// ExpandPath expands environment variables and a leading ~ in the given path.
Original file line number Diff line number Diff line change @@ -24,10 +24,19 @@ func MergeErr(errs []error) error {
2424 return fmt .Errorf ("%s" , strings .Join (msgs , ", " ))
2525}
2626
27+ // YAMLToJSON converts the first YAML document in file to JSON.
28+ // Returns an error if the reader contains more than one YAML document, as
29+ // only the first would be validated and silently ignoring later documents
30+ // can mask configuration mistakes.
2731func YAMLToJSON (file io.Reader ) ([]byte , error ) {
32+ dec := yaml .NewDecoder (file )
2833 var data interface {}
29- if err := yaml . NewDecoder ( file ) .Decode (& data ); err != nil {
34+ if err := dec .Decode (& data ); err != nil {
3035 return nil , err
3136 }
37+ var extra interface {}
38+ if err := dec .Decode (& extra ); err == nil {
39+ return nil , fmt .Errorf ("multi-document YAML is not supported for schema validation" )
40+ }
3241 return json .Marshal (data )
3342}
You can’t perform that action at this time.
0 commit comments