diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..5ad2311 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,23 @@ +name: Publish + +on: push + +jobs: + publish: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.16 + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b1b7a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +dist/ +go.sum diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..02f9b18 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,31 @@ +project_name: rocket +before: + hooks: + - go mod tidy + - go generate ./... +builds: + - env: + - CGO_ENABLED=0 + main: ./main.go + binary: rocket + goos: + - linux + - windows + - darwin +archives: + - replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8fca80f --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module quentinguidee.io/rocket-launches + +go 1.14 + +require github.com/fatih/color v1.10.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..47b5e2a --- /dev/null +++ b/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "net/http" + + "github.com/fatih/color" +) + +type Launch struct { + Name string + Details string + Rocket string + Date_utc string +} + +type Rocket struct { + Name string +} + +func main() { + flag.String("command", "next", "Command to execute") + flag.Parse() + + launch := new(Launch) + rocket := new(Rocket) + + fmt.Println() + font := color.New(color.FgHiMagenta).Add(color.Bold) + + if flag.Arg(0) == "latest" { + font.Println("=> LATEST LAUNCH\n") + GetLatestLaunch(launch) + } else { + font.Println("=> NEXT LAUNCH\n") + GetNextLaunch(launch) + } + GetRocket(rocket, launch.Rocket) + PrintLaunch(launch, rocket) +} + +func PrintLaunch(launch *Launch, rocket *Rocket) { + PrintLine("Name", launch.Name) + PrintLine("Date", launch.Date_utc) + PrintLine("Rocket", rocket.Name) + PrintLine("Description", launch.Details) +} + +func PrintLine(tag string, content string) { + titleFont := color.New(color.FgYellow).Add(color.Bold) + + titleFont.Printf(tag + " ") + fmt.Println(content) +} + +func GetLatestLaunch(launch interface{}) error { + return Request("launches/latest", launch) +} + +func GetNextLaunch(launch interface{}) error { + return Request("launches/next", launch) +} + +func GetRocket(rocket interface{}, id string) error { + return Request("rockets/"+id, rocket) +} + +func Request(path string, object interface{}) error { + resp, err := http.Get("https://api.spacexdata.com/v4/" + path) + + if err != nil { + log.Fatalln(err) + } + + defer resp.Body.Close() + + return json.NewDecoder(resp.Body).Decode(object) +}