diff --git a/README.MD b/README.MD index 45642c0..65d5746 100644 --- a/README.MD +++ b/README.MD @@ -1,3 +1,67 @@ -# GWeather +# GWeather - A Golang Weather CLI -[Coming Soon] +GWeather is a command-line interface (CLI) written in Golang that provides weather forecasts using the WeatherAPI from RapidAPI. + +## Installation + +1. Clone the repository: + ```bash + git clone https://github.com/zoelabbb/Gweather.git + ``` + +2. Navigate to the project directory: + ```bash + cd Gweather + ``` + +3. Create a `.env` file with your RapidAPI key and host: + ```env + RAPID_API_KEY=your-rapidapi-key + RAPID_API_HOST=weatherapi-com.p.rapidapi.com + ``` + +4. Build the application: + ```bash + go build -o Gweather + ``` + +5. Move the executable to your path: + ```bash + mv Gweather /usr/local/bin + ``` + +6. Run the application: + ```bash + Gweather {city} + ``` + +## Usage + +Run the application with an optional city parameter to get the weather forecast. If no city is provided, it defaults to **"Denpasar"**. + +```bash +Gweather Jakarta +``` + +## Dependencies + +- [github.com/fatih/color](https://pkg.go.dev/github.com/fatih/color) +- [github.com/joho/godotenv](https://pkg.go.dev/github.com/joho/godotenv) + +## Configuration + +Configure the application by setting your RapidAPI key and host in the `.env` file. + +## Contributing + +1. Fork the repository (https://github.com/zoelabbb/Gweather/fork) +2. Create a new branch (`git checkout -b feature-new`) +3. Commit your changes (`git commit -am 'Add new feature'`) +4. Push to the branch (`git push origin feature-new`) +5. Create a new Pull Request + +## Contact + +For questions or suggestions, feel free to contact the maintainers: + +- [Ryu as a Dev](mailto:alifryuuofficial@gmail.com) \ No newline at end of file diff --git a/go.mod b/go.mod index 1b3cdb0..64130ea 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,10 @@ module druc/sun go 1.21.4 require github.com/joho/godotenv v1.5.1 + +require ( + github.com/fatih/color v1.16.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + golang.org/x/sys v0.15.0 // indirect +) diff --git a/go.sum b/go.sum index d61b19e..761fcad 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,13 @@ +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/main.go b/main.go index c311bef..18a0cb2 100644 --- a/main.go +++ b/main.go @@ -6,36 +6,37 @@ import ( "io/ioutil" "net/http" "os" + "time" + "github.com/fatih/color" "github.com/joho/godotenv" ) // Weather structure type Weather struct { Location struct { - Name string `json:"name"` + Name string `json:"name"` Country string `json:"country"` - Region string `json:"region"` + Region string `json:"region"` } `json:"location"` Current struct { - TempC float64 `json:"temp_c"` + TempC float64 `json:"temp_c"` Condition struct { Text string `json:"text"` - Icon string `json:"icon"` - Code int `json:"code"` - }`json:"condition"` + } `json:"condition"` } `json:"current"` Forecast struct { Forecastday []struct { - Day struct { + Hour []struct { + TimeEpoch int `json:"time_epoch"` + TempC float64 `json:"temp_c"` Condition struct { Text string `json:"text"` - Icon string `json:"icon"` - Code int `json:"code"` } `json:"condition"` - } `json:"day"` + ChanceOfRain float64 `json:"chance_of_rain"` + } `json:"hour"` } `json:"forecastday"` - }`json:"forecast"` + } `json:"forecast"` } func main() { @@ -50,7 +51,12 @@ func main() { rapidAPIKey := os.Getenv("RAPID_API_KEY") rapidAPIHost := os.Getenv("RAPID_API_HOST") - url := "https://weatherapi-com.p.rapidapi.com/forecast.json?q=Indonesia&days=3" + q := "Denpasar" + if len(os.Args) >= 2 { + q = os.Args[1] + } + + url := "https://weatherapi-com.p.rapidapi.com/forecast.json?q=" + q + "&days=3" req, err := http.NewRequest("GET", url, nil) if err != nil { panic(err) @@ -85,5 +91,37 @@ func main() { panic(err) } - fmt.Println(weather) -} \ No newline at end of file + // fmt.Println(weather) + + location, curerrent, hours := weather.Location, weather.Current, weather.Forecast.Forecastday[0].Hour + + fmt.Printf("%s, %s: %.0fC, %s\n", + location.Name, + location.Region, + curerrent.TempC, + curerrent.Condition.Text) + + + for _, hour := range hours { + date := time.Unix(int64(hour.TimeEpoch), 0) + + // Time now & future + if date.Before(time.Now()) { + continue + } + + message := fmt.Sprintf( + "Time: %s - %.0fC, %s, %.0f%% chance of rain\n", + date.Format("15:04"), + hour.TempC, + hour.Condition.Text, + hour.ChanceOfRain, + ) + + if hour.ChanceOfRain < 40 { + color.Cyan(message) + } else { + color.Red(message) + } + } +}