Skip to content

Commit

Permalink
typing wip
Browse files Browse the repository at this point in the history
typing wip

Signed-off-by: Tullio Sebastiani <[email protected]>

typing wip

typing wip
  • Loading branch information
tsebastiani committed Oct 10, 2024
1 parent 7c7d880 commit 249722b
Show file tree
Hide file tree
Showing 449 changed files with 214,454 additions and 1,808 deletions.
28 changes: 28 additions & 0 deletions cmd/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
)

var describeCmd = &cobra.Command{
Use: "describe",
Short: "describes a scenario",
Long: `Describes a scenario`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"mimmo", "memma"}, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
if jsonFlag {
fmt.Println("{\"describe\":\"" + args[0] + "\"}")
} else {
fmt.Println("Listing scenarios " + args[0])
}
return nil
},
}

func init() {
rootCmd.AddCommand(describeCmd)
}
36 changes: 36 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"fmt"
"github.com/briandowns/spinner"
"github.com/spf13/cobra"
"time"
)

var listCmd = &cobra.Command{
Use: "list",
Short: "List scenarios",
Long: `List available krkn-hub scenarios`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
s := spinner.New(spinner.CharSets[39], 100*time.Millisecond)
s.Suffix = "fetching scenarios"
s.Start()
time.Sleep(4 * time.Second)
s.Stop()
if offlineFlag {
fmt.Println("OFFLINE MODE")
}
if jsonFlag {
fmt.Println("{\"hello\":\"list\"}")
return nil
} else {
fmt.Println("Listing scenarios...")
return nil
}
},
}

func init() {
rootCmd.AddCommand(listCmd)
}
29 changes: 0 additions & 29 deletions cmd/mix.go

This file was deleted.

13 changes: 10 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ import (
"os"
)

var jsonFlag bool
var offlineFlag bool
var offlineRepoConfig string
var rootCmd = &cobra.Command{
Use: "mixologist",
Short: "Mixologist is your personal bartender.",
Long: `Mixologist acts as a bartender who specializes in cocktail making.`,
Use: "krknctl",
Short: "krkn CLI",
Long: `krkn Command Line Interface`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

func Execute() {
rootCmd.PersistentFlags().BoolVarP(&jsonFlag, "json", "j", false, "Output in JSON")
rootCmd.PersistentFlags().BoolVarP(&offlineFlag, "offline", "o", false, "Offline mode")
rootCmd.PersistentFlags().StringVarP(&offlineRepoConfig, "offline-repo-config", "r", "", "Offline repository config file")
rootCmd.MarkFlagsRequiredTogether("offline", "offline-repo-config")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ module github.com/krkn-chaos/krknctl
go 1.23.1

require (
github.com/briandowns/spinner v1.23.1
github.com/spf13/cobra v1.8.1
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/term v0.1.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 21 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
github.com/briandowns/spinner v1.23.1 h1:t5fDPmScwUjozhDj4FA46p5acZWIPXYE30qW2Ptu650=
github.com/briandowns/spinner v1.23.1/go.mod h1:LaZeM4wm2Ywy6vO571mvhQNRcWfRUnXOs0RcKV0wYKM=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
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/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
24 changes: 24 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package config

import (
_ "embed"
"encoding/json"
)

type Config struct {
Version string `json:"version"`
QuayRegistry string `json:"quayRegistry"`
QuayApi string `json:"quayApi"`
}

//go:embed config.json
var File []byte

func LoadConfig() (Config, error) {
var config Config
err := json.Unmarshal(File, &config)
if err != nil {
return config, err
}
return config, nil
}
5 changes: 5 additions & 0 deletions internal/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": "0.0.1",
"quayRegistry": "krkn-chaos/krkn-hub",
"quayApi": "https://quay.io/api/v1/repository"
}
14 changes: 14 additions & 0 deletions internal/providers/offline_scenario_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package providers

import (
"errors"
"github.com/krkn-chaos/krknctl/internal/config"
)

type OfflineScenarioProvider struct {
config *config.Config
}

func (p *OfflineScenarioProvider) GetScenarios() (*[]ScenarioTag, error) {
return nil, errors.New("not yet implemented")
}
30 changes: 30 additions & 0 deletions internal/providers/provider_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package providers

import "github.com/krkn-chaos/krknctl/internal/config"

type Mode int64

const (
Online = iota
Offline
)

type ProviderFactory struct {
config *config.Config
}

func NewProviderFactory(config *config.Config) *ProviderFactory {
return &ProviderFactory{config: config}
}

func (p *ProviderFactory) NewInstance(mode Mode) ScenarioProvider {
switch mode {
case Online:

return &QuayScenarioProvider{config: p.config}
case Offline:
return &OfflineScenarioProvider{config: p.config}
default:
return nil
}
}
119 changes: 119 additions & 0 deletions internal/providers/quay_scenario_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package providers

import (
"encoding/json"
"errors"
"github.com/krkn-chaos/krknctl/internal/config"
"io"
"log"
"net/http"
"net/url"
"time"
)

type QuayTagPage struct {
Tags []QuayTag `json:"tags"`
Page int `json:"page"`
HasAdditional bool `json:"has_additional"`
}

type QuayTag struct {
Name string `json:"name"`
Reversion bool `json:"reversion"`
StartTimeStamp int64 `json:"start_ts"`
ManifestDigest string `json:"manifest_digest"`
IsManifestList bool `json:"is_manifest_list"`
Size int64 `json:"size"`
LastModified time.Time `json:"last_modified"`
}

type alias QuayTag

func (q *QuayTag) UnmarshalJSON(bytes []byte) error {
aux := &struct {
LastModified string `json:"last_modified"`
*alias
}{
alias: (*alias)(q),
}
if err := json.Unmarshal(bytes, &aux); err != nil {
return err
}

layout := "Mon, 02 Jan 2006 15:04:05 -0700"
parsedTime, err := time.Parse(layout, aux.LastModified)
if err != nil {
return err
}

q.LastModified = parsedTime

return nil
}

type QuayScenarioProvider struct {
config *config.Config
}

func (p *QuayScenarioProvider) GetScenarios() (*[]ScenarioTag, error) {
baseURL, _ := url.Parse(p.config.QuayApi + "/" + p.config.QuayRegistry + "/tag")
params := url.Values{}
params.Add("onlyActiveTags", "true")
params.Add("limit", "100")
// currently paging support is not needed
params.Add("page", "1")
baseURL.RawQuery = params.Encode()

resp, _ := http.Get(baseURL.String())
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New("failed to retrieve tags, " + baseURL.String() + " returned: " + resp.Status)
}

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return nil, err
}
var quayPage QuayTagPage
err = json.Unmarshal(bodyBytes, &quayPage)

var scenarioTags []ScenarioTag
for _, tag := range quayPage.Tags {
scenarioTags = append(scenarioTags, ScenarioTag{
Name: tag.Name,
LastModified: tag.LastModified,
Size: tag.Size,
Digest: tag.ManifestDigest,
})
}

return &scenarioTags, nil
}

func (p *QuayScenarioProvider) GetScenarioDetail(scenario string) (*ScenarioDetail, error) {
scenarios, err := p.GetScenarios()
if err != nil {
return nil, err
}
var foundScenario *ScenarioTag = nil
for _, scenarioTag := range *scenarios {
if scenarioTag.Name == scenario {
foundScenario = &scenarioTag
}
}
if foundScenario == nil {
return nil, nil
}

baseURL, _ := url.Parse(p.config.QuayApi + "/" + p.config.QuayRegistry + "/tag")
params := url.Values{}
params.Add("onlyActiveTags", "true")
params.Add("limit", "100")
// currently paging support is not needed
params.Add("page", "1")
baseURL.RawQuery = params.Encode()

resp, _ := http.Get(baseURL.String())

}
Loading

0 comments on commit 249722b

Please sign in to comment.