-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for searching images that are available.
- Loading branch information
Showing
15 changed files
with
455 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ImagesListOutput []ImagesListOutput_Item | ||
|
||
type ImagesListOutput_Item struct { | ||
Source string `json:"source"` | ||
Name string `json:"name"` | ||
} | ||
|
||
var imagesListCmd = &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
Short: "Lists all the images available locally", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
helper := CmdHelper{} | ||
logger := helper.GetLogger() | ||
ctx := helper.GetContext() | ||
|
||
outputJson, _ := cmd.Flags().GetBool("json") | ||
|
||
deployer := helper.GetDeployer(ctx) | ||
images, err := deployer.ListImages(ctx) | ||
if err != nil { | ||
logger.Fatal("failed to list images", zap.Error(err)) | ||
} | ||
|
||
if !outputJson { | ||
fmt.Printf("Images:\n") | ||
for _, image := range images { | ||
fmt.Printf(" %s [Source: %s]\n", image.Name, image.Source) | ||
} | ||
} else { | ||
var out ImagesListOutput | ||
for _, image := range images { | ||
out = append(out, ImagesListOutput_Item{ | ||
Source: image.Source, | ||
Name: image.Name, | ||
}) | ||
} | ||
helper.OutputJson(out) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
imagesCmd.AddCommand(imagesListCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/couchbaselabs/cbdinocluster/deployment" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ImagesSearchOutput []ImagesSearchOutput_Item | ||
|
||
type ImagesSearchOutput_Item struct { | ||
Source string `json:"source"` | ||
Name string `json:"name"` | ||
} | ||
|
||
var imagesSearchCmd = &cobra.Command{ | ||
Use: "search", | ||
Aliases: []string{"find"}, | ||
Short: "Searches all the images available to use", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
helper := CmdHelper{} | ||
logger := helper.GetLogger() | ||
ctx := helper.GetContext() | ||
|
||
outputJson, _ := cmd.Flags().GetBool("json") | ||
limit, _ := cmd.Flags().GetInt("limit") | ||
|
||
deployer := helper.GetDeployer(ctx) | ||
images, err := deployer.SearchImages(ctx, args[0]) | ||
if err != nil { | ||
logger.Fatal("failed to search images", zap.Error(err)) | ||
} | ||
|
||
if !outputJson { | ||
// we want dockerhub images before ghcr images | ||
getSourceKey := func(source string) string { | ||
if source == "ghcr" { | ||
return "4" | ||
} else if source == "dockerhub" { | ||
return "6" | ||
} | ||
return "5" | ||
} | ||
|
||
// We sort backwards to put new versions at the top | ||
slices.SortFunc(images, func(a deployment.Image, b deployment.Image) int { | ||
return -strings.Compare( | ||
getSourceKey(a.Source)+a.Name, | ||
getSourceKey(b.Source)+b.Name) | ||
}) | ||
|
||
fmt.Printf("Images:\n") | ||
for imageNum, image := range images { | ||
if limit > 0 && imageNum > limit { | ||
fmt.Printf(" ...\n") | ||
break | ||
} | ||
|
||
fmt.Printf(" %s [Source: %s]\n", image.Name, image.Source) | ||
} | ||
} else { | ||
var out ImagesSearchOutput | ||
for imageNum, image := range images { | ||
if limit > 0 && imageNum > limit { | ||
break | ||
} | ||
|
||
out = append(out, ImagesSearchOutput_Item{ | ||
Source: image.Source, | ||
Name: image.Name, | ||
}) | ||
} | ||
helper.OutputJson(out) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
imagesCmd.AddCommand(imagesSearchCmd) | ||
|
||
imagesSearchCmd.Flags().Int("limit", 10, "The maximum number of results to return") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var imagesCmd = &cobra.Command{ | ||
Use: "images", | ||
Short: "Provides the ability to list/search server version images.", | ||
Run: nil, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(imagesCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.