github.com/shravanasati/hydra@v1.0.1-0.20240122045627-1082d2ed50d2/src/config.go (about) 1 /* 2 The following code is responsible for the config command. 3 4 Author: Shravan Asati 5 Originally Written: 30 March 2021 6 Last edited: 13 April 2021 7 */ 8 9 package main 10 11 import ( 12 "bufio" 13 "encoding/json" 14 "fmt" 15 "os" 16 "os/user" 17 "path/filepath" 18 ) 19 20 type Configuration struct { 21 FullName string `json:"FullName"` 22 GithubUsername string `json:"GithubUsername"` 23 DefaultLang string `json:"DefaultLang"` 24 DefaultLicense string `json:"DefaultLicense"` 25 } 26 27 func jsonifyConfig(config *Configuration) string { 28 byteArray, err := json.Marshal(config) 29 if err != nil { 30 panic(err) 31 } 32 return string(byteArray) 33 } 34 35 func readConfig(jsonString string) *Configuration { 36 var result *Configuration 37 err := json.Unmarshal([]byte(jsonString), &result) 38 if err != nil { 39 handleException(err) 40 } 41 return result 42 } 43 44 func getConfig(value string) string { 45 // to make sure the config file exists 46 config("default", "default", "default", "default") 47 48 usr, _ := user.Current() 49 configFile := (filepath.Join(usr.HomeDir, ".hydra/config.json")) 50 file, ferr := os.Open(configFile) 51 handleException(ferr) 52 defer file.Close() 53 wholeText := "" 54 scanner := bufio.NewScanner(file) 55 for scanner.Scan() { 56 line := scanner.Text() 57 wholeText = wholeText + line 58 } 59 60 config := readConfig(wholeText) 61 switch value { 62 case "fullName": 63 return config.FullName 64 case "githubUsername": 65 return config.GithubUsername 66 case "defaultLang": 67 return config.DefaultLang 68 case "defaultLicense": 69 return config.DefaultLicense 70 default: 71 return fmt.Sprintf("Undefined value: %v.", value) 72 } 73 } 74 75 func checkForCorrectConfig() bool { 76 // to make sure the config file exists 77 config("default", "default", "default", "default") 78 79 usr, _ := user.Current() 80 configFile := (filepath.Join(usr.HomeDir, ".hydra/config.json")) 81 file, ferr := os.Open(configFile) 82 handleException(ferr) 83 wholeText := "" 84 scanner := bufio.NewScanner(file) 85 for scanner.Scan() { 86 line := scanner.Text() 87 wholeText = wholeText + line 88 } 89 file.Close() 90 91 config := readConfig(wholeText) 92 93 if config.FullName == "" || config.GithubUsername == "" { 94 return false 95 } else { 96 return true 97 } 98 } 99 100 func exists(path string) (bool, error) { 101 _, err := os.Stat(path) 102 if err == nil { 103 return true, nil 104 } 105 if os.IsNotExist(err) { 106 return false, nil 107 } 108 return false, err 109 } 110 111 func config(fullName, githubUsername, defaultLang, defaultLicense string) { 112 // * defining path of hydra config file 113 usr, _ := user.Current() 114 hydraDir := filepath.Join(usr.HomeDir, ".hydra") 115 116 if pathOk, _ := exists(hydraDir); !pathOk { 117 os.Mkdir(hydraDir, os.ModePerm) 118 } 119 120 configFile := filepath.Join(hydraDir, "config.json") 121 122 // * creating a file in case it doesnt exists 123 if configOk, _ := exists(configFile); !configOk { 124 f, err := os.Create(configFile) 125 handleException(err) 126 defaultConfig := Configuration{FullName: "", GithubUsername: "", DefaultLang: "", DefaultLicense: "MIT"} 127 _, er := f.WriteString(jsonifyConfig(&defaultConfig)) 128 handleException(er) 129 f.Close() 130 } 131 132 // * reading data from the file 133 file, ferr := os.Open(configFile) 134 handleException(ferr) 135 defer file.Close() 136 wholeText := "" 137 scanner := bufio.NewScanner(file) 138 for scanner.Scan() { 139 line := scanner.Text() 140 wholeText = wholeText + line 141 } 142 143 // * writing new config to the file by first deleting it 144 configStruct := readConfig(wholeText) 145 if fullName != "default" { 146 configStruct.FullName = fullName 147 fmt.Printf("Successfully configured the full name to '%v'. \n", fullName) 148 } 149 150 if githubUsername != "default" { 151 configStruct.GithubUsername = githubUsername 152 fmt.Printf("Successfully configured the GitHub username to '%v'. \n", githubUsername) 153 } 154 155 if defaultLang != "default" { 156 configStruct.DefaultLang = defaultLang 157 fmt.Printf("Successfully configured the default language to '%v'. \n", defaultLang) 158 } 159 160 if defaultLicense != "default" { 161 configStruct.DefaultLicense = defaultLicense 162 fmt.Printf("Successfully configured the default license to '%v'. \n", defaultLicense) 163 } 164 165 os.Remove(configFile) 166 f, err := os.Create(configFile) 167 handleException(err) 168 _, er := f.WriteString(jsonifyConfig(configStruct)) 169 handleException(er) 170 f.Close() 171 }