Skip to content

Commit

Permalink
Merge pull request #58 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 0.4.2
  • Loading branch information
andyone authored Apr 1, 2024
2 parents 6f66848 + fa73c4c commit c462d6b
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 220 deletions.
96 changes: 78 additions & 18 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@ package app

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"
"os"
"strconv"
"time"

"github.com/essentialkaos/ek/v12/fmtc"
"github.com/essentialkaos/ek/v12/fmtutil"
"github.com/essentialkaos/ek/v12/fsutil"
"github.com/essentialkaos/ek/v12/options"
"github.com/essentialkaos/ek/v12/req"
"github.com/essentialkaos/ek/v12/spinner"
"github.com/essentialkaos/ek/v12/support"
"github.com/essentialkaos/ek/v12/support/deps"
"github.com/essentialkaos/ek/v12/terminal/tty"
"github.com/essentialkaos/ek/v12/timeutil"
"github.com/essentialkaos/ek/v12/tmp"
"github.com/essentialkaos/ek/v12/usage"
"github.com/essentialkaos/ek/v12/usage/completion/bash"
"github.com/essentialkaos/ek/v12/usage/completion/fish"
"github.com/essentialkaos/ek/v12/usage/completion/zsh"
"github.com/essentialkaos/ek/v12/usage/man"
"github.com/essentialkaos/ek/v12/usage/update"

"github.com/essentialkaos/artefactor/app/support"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// Basic application info
const (
APP = "artefactor"
VER = "0.4.1"
VER = "0.4.2"
DESC = "Utility for downloading artefacts from GitHub"
)

Expand Down Expand Up @@ -101,7 +105,11 @@ func Run(gitRev string, gomod []byte) {
genAbout(gitRev).Print(options.GetS(OPT_VER))
os.Exit(0)
case options.GetB(OPT_VERB_VER):
support.Print(APP, VER, gitRev, gomod)
support.Collect(APP, VER).
WithRevision(gitRev).
WithDeps(deps.Extract(gomod)).
WithChecks(checkGithubAvailability()...).
Print()
os.Exit(0)
case options.GetB(OPT_HELP) || len(args) == 0:
genUsage().Print()
Expand Down Expand Up @@ -134,11 +142,7 @@ func Run(gitRev string, gomod []byte) {

// preConfigureUI preconfigures UI based on information about user terminal
func preConfigureUI() {
if !fmtc.IsColorsSupported() {
fmtc.DisableColors = true
}

if os.Getenv("NO_COLOR") != "" {
if !tty.IsTTY() {
fmtc.DisableColors = true
}

Expand Down Expand Up @@ -211,16 +215,72 @@ func printError(f string, a ...interface{}) {
}
}

// printError prints warning message to console
func printWarn(f string, a ...interface{}) {
if len(a) == 0 {
fmtc.Fprintln(os.Stderr, "{y}▲ "+f+"{!}")
} else {
fmtc.Fprintf(os.Stderr, "{y}▲ "+f+"{!}\n", a...)
// ////////////////////////////////////////////////////////////////////////////////// //

// checkGithubAvailability checks GitHub API availability
func checkGithubAvailability() []support.Check {
var chks []support.Check

headers := req.Headers{"X-GitHub-Api-Version": _API_VERSION}

if options.Has(OPT_TOKEN) {
headers["Authorization"] = "Bearer " + options.GetS(OPT_TOKEN)
}
}

// ////////////////////////////////////////////////////////////////////////////////// //
resp, err := req.Request{
URL: "https://api.github.com/octocat",
Headers: headers,
AutoDiscard: true,
}.Get()

if err != nil {
chks = append(chks, support.Check{
support.CHECK_ERROR, "GitHub API", "Can't send request",
})
return chks
} else if resp.StatusCode != 200 {
chks = append(chks, support.Check{
support.CHECK_ERROR, "GitHub API", fmt.Sprintf(
"API returned non-ok status code %s", resp.StatusCode,
),
})
return chks
}

chks = append(chks, support.Check{
support.CHECK_OK, "GitHub API", "API available",
})

remaining := resp.Header.Get("X-Ratelimit-Remaining")
used := resp.Header.Get("X-Ratelimit-Used")
limit := resp.Header.Get("X-Ratelimit-Limit")
resetTS, _ := strconv.ParseInt(resp.Header.Get("X-Ratelimit-Reset"), 10, 64)
resetDate := time.Unix(resetTS, 0)

chk := support.Check{
Title: "API Ratelimit",
Message: fmt.Sprintf(
"(Used: %s/%s | Reset: %s)",
used, limit, timeutil.Format(resetDate, "%Y/%m/%d %H:%M:%S"),
),
}

switch remaining {
case "":
chk.Status = support.CHECK_WARN
chk.Message = "No info about ratelimit"
case "0":
chk.Status = support.CHECK_ERROR
chk.Message = "No remaining requests " + chk.Message
default:
chk.Status = support.CHECK_OK
chk.Message = "OK " + chk.Message
}

chks = append(chks, chk)

return chks
}

// printCompletion prints completion for given shell
func printCompletion() int {
Expand Down
2 changes: 1 addition & 1 deletion app/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
6 changes: 3 additions & 3 deletions app/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -87,7 +87,7 @@ func downloadArtefact(artefact *Artefact, dataDir string) error {
outputFile := path.Join(releaseDir, artefact.Output)

if fsutil.IsExist(outputFile) {
fmtc.Println(" {g}The latest version already downloaded{!}")
fmtc.Println(" {s}There is no update available for this application{!}")
return nil
}

Expand All @@ -110,7 +110,7 @@ func downloadArtefact(artefact *Artefact, dataDir string) error {
binarySize := fsutil.GetSize(outputFile)

fmtc.Printf(
" {g}Artefact successfully downloaded {s}%s{g} and saved to data directory{!}\n",
" {g}Artefact successfully downloaded (%s) and saved to data directory{!}\n",
fmtutil.PrettySize(binarySize),
)

Expand Down
2 changes: 1 addition & 1 deletion app/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion app/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
189 changes: 0 additions & 189 deletions app/support/support.go

This file was deleted.

Loading

0 comments on commit c462d6b

Please sign in to comment.