8000 cmd: list app versions by matslina · Pull Request #972 · tidbyt/pixlet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

cmd: list app versions #972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 69 additions & 18 deletions cmd/private/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type TidbytApp struct {
OrganizationID string `json:"organizationID,omitempty"`
}

type TidbytAppVersion struct {
ID string `json:"id"`
Created string `json:"created,omitempty"`
}

var listURL string

func init() {
Expand All @@ -27,15 +32,26 @@ func init() {

var ListCmd = &cobra.Command{
Use: "list",
Short: "Lists private apps",
Long: `Lists private apps, including team apps.`,
Short: "Lists private apps and versions",
Long: `Lists private apps, or available versions of a single private app.`,
RunE: func(cmd *cobra.Command, args []string) error {
apiToken := config.OAuthTokenFromConfig(cmd.Context())
if apiToken == "" {
return fmt.Errorf("login with `pixlet login` or use `pixlet set-auth` to configure auth")
}

requestURL := fmt.Sprintf("%s/v0/apps", listURL)
appID := ""
if len(args) > 0 {
appID = args[0]
}

var requestURL string
if appID != "" {
requestURL = fmt.Sprintf("%s/v0/apps/%s/versions", listURL, appID)
} else {
requestURL = fmt.Sprintf("%s/v0/apps", listURL)
}

req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return fmt.Errorf("could not create http request: %w", err)
Expand All @@ -58,30 +74,65 @@ var ListCmd = &cobra.Command{
if resp.StatusCode != 200 {
return fmt.Errorf("request returned status %d with message: %s", resp.StatusCode, body)
}

if err != nil {
return fmt.Errorf("could not read response body: %w", err)
}

var apps struct {
Apps []TidbytApp `json:"apps"`
}
err = json.Unmarshal(body, &apps)
if err != nil {
return fmt.Errorf("could not parse response body: %w", err)
}

for _, app := range apps.Apps {
if !app.Private {
continue
if appID != "" {
err = listVersions(body)
if err != nil {
return fmt.Errorf("could not list versions: %w", err)
}
appJson, err := json.Marshal(app)
} else {
err = listApps(body)
if err != nil {
return fmt.Errorf("could not marshal app: %w", err)
return fmt.Errorf("could not list apps: %w", err)
}
fmt.Println(string(appJson))
}

return nil
},
}

func listApps(body []byte) error {
var apps struct {
Apps []TidbytApp `json:"apps"`
}
err := json.Unmarshal(body, &apps)
if err != nil {
return fmt.Errorf("could not parse response body: %w", err)
}

for _, app := range apps.Apps {
if !app.Private {
continue
}
appJson, err := json.Marshal(app)
if err != nil {
return fmt.Errorf("could not marshal app: %w", err)
}
fmt.Println(string(appJson))
}

return nil
}

func listVersions(body []byte) error {
var versions struct {
Versions []TidbytAppVersion `json:"versions"`
}
err := json.Unmarshal(body, &versions)
if err != nil {
return fmt.Errorf("could not parse response body: %w", err)
}

for _, version := range versions.Versions {
versionJson, err := json.Marshal(version)
if err != nil {
return fmt.Errorf("could not marshal version: %w", err)
}
fmt.Println(string(versionJson))
}

return nil
}
0