Add flag parsing for --file and --url

This commit is contained in:
Sol Fisher Romanoff
2023-09-01 19:12:02 +03:00
parent 11bfcb582a
commit fdb46284f6
4 changed files with 55 additions and 4 deletions
+41
View File
@@ -0,0 +1,41 @@
package main
import (
"bufio"
"os"
"github.com/charmbracelet/log"
"github.com/spf13/pflag"
)
func parseURLs() []string {
var url = pflag.StringArrayP("url", "u", []string{}, "URL to check")
var file = pflag.StringP("file", "f", "", "File that includes URLs to check")
pflag.Parse()
if *url != nil {
return *url
} else if *file != "" {
if _, err := os.Stat(*file); err != nil {
log.Fatal(err)
}
log.Infof("Reading file %s", *file)
data, err := os.Open(*file)
if err != nil {
log.Fatal(err)
}
defer data.Close()
var urls []string
scanner := bufio.NewScanner(data)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
urls = append(urls, scanner.Text())
}
return urls
}
return []string{}
}
+7 -3
View File
@@ -1,11 +1,15 @@
module github.com/pushfs/sif
go 1.21.0
go 1.20
require (
github.com/charmbracelet/lipgloss v0.8.0
github.com/charmbracelet/log v0.2.4
github.com/spf13/pflag v1.0.5
)
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.8.0 // indirect
github.com/charmbracelet/log v0.2.4 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
+2
View File
@@ -20,6 +20,8 @@ github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1n
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+5 -1
View File
@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
)
@@ -27,4 +26,9 @@ func main() {
fmt.Println(subline.Render("https://sif.sh - man's best friend"))
log.Info("Hello World!")
urls := parseURLs()
for _, url := range urls {
log.Infof("Looking up %s...", url)
}
}