Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
[all] Rocket CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinguidee committed Apr 15, 2021
0 parents commit d8237e0
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
go.sum
31 changes: 31 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -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:'
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module quentinguidee.io/rocket-launches

go 1.14

require github.com/fatih/color v1.10.0
81 changes: 81 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit d8237e0

Please sign in to comment.