-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmain.go
More file actions
181 lines (162 loc) · 4.89 KB
/
main.go
File metadata and controls
181 lines (162 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bufio"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/ashish0kumar/stormy/internal/weather"
"github.com/fatih/color"
"github.com/k0kubun/go-ansi"
"golang.org/x/term"
)
// version is set during build time using -ldflags
var version = "dev"
func init() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
stop := <-c
// reset cursor visibility for live mode
_, _ = ansi.Print("\x1b[?25h")
switch stop {
case os.Interrupt, syscall.SIGTERM:
_, _ = fmt.Fprintf(
os.Stderr, "\n\n[%s] Program interrupted. Bye!\n", color.New(color.FgRed).SprintFunc()("x"),
)
os.Exit(1)
case syscall.SIGQUIT:
fmt.Printf("\n\n[%s] Stopping now, bye!\n", color.New(color.FgGreen).SprintFunc()("✓"))
os.Exit(0)
}
}()
}
func listenForQuit(stop chan struct{}) {
var shouldExit, shouldInterrupt bool
// Switch stdin into 'raw' mode
oldState, errRaw := term.MakeRaw(int(os.Stdin.Fd()))
if errRaw != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error setting raw mode: %v\n", errRaw)
return
}
defer func(fd int, state *term.State) {
_ = term.Restore(fd, state)
if shouldExit {
_, _ = ansi.Print("\x1b[?25h") // restore cursor
fmt.Printf("\n\n[%s] Stopping now, bye!\n", color.New(color.FgGreen).SprintFunc()("✓"))
os.Exit(0)
}
if shouldInterrupt {
_, _ = ansi.Print("\x1b[?25h") // restore cursor
_, _ = fmt.Fprintf(
os.Stderr, "\n\n[%s] Program interrupted. Bye!\n", color.New(color.FgRed).SprintFunc()("x"),
)
os.Exit(1)
}
}(int(os.Stdin.Fd()), oldState)
buffer := make([]byte, 1)
for {
select {
case <-stop:
return
default:
n, err := os.Stdin.Read(buffer)
if err != nil || n == 0 {
continue
}
char := buffer[0]
if char == 'q' || char == 'Q' {
shouldExit = true
return
}
if char == 3 { // Ctrl+C
shouldInterrupt = true
return
}
}
}
}
func main() {
// Parse command line flags
flags := weather.ParseFlags()
// Handle version flag
if flags.Version {
fmt.Printf("stormy version %s\n", version)
os.Exit(0)
}
// Read/create config
config := weather.ReadConfig()
// Override config with command line flags if provided
preFlagsConfig := config
weather.ApplyFlags(&config, flags)
scanner := bufio.NewScanner(os.Stdin)
// Check if the city is set
if config.City == "" {
fmt.Printf("No city found in your configuration, please enter the city to check the weather for: ")
scanner.Scan()
newCity := scanner.Text()
config.City = newCity
preFlagsConfig.City = newCity
err := weather.WriteConfig(preFlagsConfig, weather.GetConfigPath())
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to update your stored config: %v\n", err)
_, _ = fmt.Fprintln(os.Stderr, "You'll need to enter your city back next time if still not provided through the configuration or command line flags")
_, _ = fmt.Fprintln(os.Stderr, "Proceeding with the provided city")
}
}
// Check if the API key and city are set
if config.Provider == weather.ProviderOpenWeatherMap && config.ApiKey == "" {
fmt.Printf("No API key provided for %s, please enter it: ", config.Provider)
scanner.Scan()
apiKey := scanner.Text()
config.ApiKey = apiKey
preFlagsConfig.ApiKey = apiKey
err := weather.WriteConfig(preFlagsConfig, weather.GetConfigPath())
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to update your stored config: %v\n", err)
_, _ = fmt.Fprintln(os.Stderr, "You'll need to enter your API key back next time if still not provided through the configuration or command line flags")
_, _ = fmt.Fprintln(os.Stderr, "Proceeding with the provided API key")
}
}
fetchAndDisplay(config, false)
}
// fetchAndDisplay fetches weather data and displays it according to the given configuration.
// clearDisplay determines whether the screen should be cleared before displaying updated information.
func fetchAndDisplay(config weather.Config, clearDisplay bool) {
// Fetch weather data
weatherData, err := weather.FetchWeather(config)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to fetch weather data: %v\n", err)
if errors.Is(err, weather.ErrUnsupportedQuery) {
_, _ = fmt.Fprintf(
os.Stderr, "Detailed queries are not supported by %s, try using other providers.\n",
weather.ProviderOpenMeteo,
)
} else {
_, _ = fmt.Fprintln(os.Stderr, "Please check your internet connection and API key.")
}
os.Exit(1)
}
// Clear screen in live mode
if clearDisplay {
_, _ = ansi.Printf("\x1b[%dA\x1b[J", 7) // maximum number of displayed lines
}
// Display the weather
weather.DisplayWeather(weatherData, config)
// Loop in live mode
if !config.LiveMode {
return
}
if !clearDisplay {
// hide cursor on live mode startup
_, _ = ansi.Print("\x1b[?25l")
}
// handle q press
stop := make(chan struct{})
go listenForQuit(stop)
time.Sleep(15 * time.Second)
stop <- struct{}{}
fetchAndDisplay(config, true)
}