diff --git a/main.go b/main.go index dd7608d..aebcc1f 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,7 @@ type Main struct { type Wind struct { Speed float64 `json:"speed"` Deg float64 `json:"deg"` - gust float64 `json:"gust"` + Gust float64 `json:"gust"` } type Rain struct { @@ -72,15 +72,13 @@ type Sys struct { func main() { //set up the flags for the program to use! - var city, code string - flag.StringVar(&city, "city", "London", "Enter city e.g. London, Glasgow etc...") - flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk, us etc...") - + var city, code, api_key string + flag.StringVar(&city, "city", "London", "Enter city e.g. London, Glasgow etc... (Default: London)") + flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk, us etc... (Default: uk)") + flag.StringVar(&code, "api", "none", "Enter Code e.g. uk, us etc... Default(none)") flag.Parse() - //setting up openweathermap api - api_key := "29e5139b86638988d333a28ba360bfd9" + full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&appid=" + api_key + "" - fmt.Printf("\n------\nWeather url: %+s\n---------\n", full) //read webpage url := full @@ -90,16 +88,13 @@ func main() { if err != nil { panic(err) } - //print the html - fmt.Printf("%s\n", html) - fmt.Println("---------- JSON PARSED -----------\n\n") - - //output the html to stirling.json - CreateFile(string(html)) + //Create file + CreateFile(string(html), false) data, _ := LoadConfiguration("weather.json") fmt.Println(data) } + func LoadConfiguration(file string) (WeatherAPI, error) { var config WeatherAPI configFile, err := os.Open(file) @@ -110,39 +105,25 @@ func LoadConfiguration(file string) (WeatherAPI, error) { jsonParser.Decode(&config) return config, err } -func CreateFile(fileContents string) { - // fmt package implements formatted - // I/O and has functions like Printf - // and Scanf - fmt.Printf("Writing to a file in Go lang\n") +func CreateFile(fileContents string, debugFlag bool) { - // in case an error is thrown it is received - // by the err variable and Fatalf method of - // log prints the error message and stops - // program execution + fmt.Printf("Writing to a file in Go lang\n") file, err := os.Create("weather.json") - if err != nil { log.Fatalf("failed creating file: %s", err) } - // Defer is used for purposes of cleanup like - // closing a running file after the file has - // been written and main //function has - // completed execution defer file.Close() - // len variable captures the length - // of the string written to the file. len, err := file.WriteString(fileContents) - if err != nil { log.Fatalf("failed writing to file: %s", err) } - // Name() method returns the name of the - // file as presented to Create() method. - fmt.Printf("\nFile Name: %s", file.Name()) - fmt.Printf("\nLength: %d bytes", len) + // Debug Flag + if debugFlag { + fmt.Printf("\nFile Name: %s", file.Name()) + fmt.Printf("\nLength: %d bytes", len) + } }