Skip to content
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

adding custom command #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ Set the latest version:
spin verman set latest
```

Create/update a pointer to a custom version:

```sh
spin verman set custom --file path/to/spin/binary

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do paths not have to be absolute?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to @karthik2804's point, it would be great to have something along the lines of:

spin verman set <user-chooses-name> /path

verman would know that it is a custom entry due to the path being provided

```

If you have already created a pointer to a custom version, you can use this to switch the Spin version:

```sh
spin verman set custom
```

## Update a version of Spin in the `~/.spin_verman` directory

```sh
Expand Down
3 changes: 2 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func exists(path string) (bool, error) {
return false, err
}

// getVersionDir returns the directory in which the "spin verman" version files will be stored
// getVersionDir returns the directory in which the "spin verman" version files will be stored.
// If the directory doesn't exist, this method will create the directory.
func getVersionDir() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func list() (string, error) {
var output []string

for _, file := range files {
if strings.HasPrefix(file.Name(), "v") || file.Name() == "canary" {
if file.Name() != "current_version" {
output = append(output, file.Name())
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var removeCmd = &cobra.Command{

version := args[0]

if !strings.HasPrefix(version, "v") && version != "canary" {
if !strings.HasPrefix(version, "v") && version != "canary" && version != "custom" {
version = "v" + version
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func Execute() {
func init() {
// Set
setCmd.AddCommand(setLatestStableCmd)
setCustomCmd.PersistentFlags().StringP("file", "f", "", "Specifies the path to the desired Spin binary")
setCmd.AddCommand(setCustomCmd)
rootCmd.AddCommand(setCmd)
// Get
getCmd.AddCommand(getLatestStableCmd)
Expand Down
98 changes: 98 additions & 0 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ var setLatestStableCmd = &cobra.Command{
},
}

var setCustomCmd = &cobra.Command{
Use: "custom",
Short: "Sets Spin to a custom binary.",
Long: "Sets Spin to a custom binary. If a pointer to the custom binary was not previously created, or you wish to update the existing custom binary, run this command with the \"--file\" flag.",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
binaryPath, err := cmd.Flags().GetString("file")
if err != nil {
return err
}

if err := setCustom(binaryPath); err != nil {
return err
}

fmt.Println("Spin has been updated to version custom")

return nil
},
}

// updateSpinBinary creates a symlink pointing to a binary file containing the specified version of Spin
func updateSpinBinary(directory, version string) error {
if err := os.MkdirAll(path.Join(directory, "current_version"), 0755); err != nil {
Expand All @@ -90,6 +111,11 @@ func updateSpinBinary(directory, version string) error {
return err
}

// If the user has defined a custom version, the checks below don't apply
if version == "custom" {
return nil
}

testSpinVersionCmd := exec.Command("spin", "--version")
currentSpinVersionBytes, err := testSpinVersionCmd.CombinedOutput()
if err != nil {
Expand Down Expand Up @@ -119,3 +145,75 @@ func updateSpinBinary(directory, version string) error {

return nil
}

// setCustom creates or updates the symlink pointing to a binary file containing a custom version of Spin found locally.
// If the user has already created the symlink, this will switch Spin to the existing symlink.
func setCustom(pathToSpinBinary string) error {
versionDir, err := getVersionDir()
if err != nil {
return err
}

if pathToSpinBinary == "" {
binaryExists, err := exists(path.Join(versionDir, "custom", "spin"))
if err != nil {
return err
}

if !binaryExists {
return fmt.Errorf("nothing points to a custom Spin binary, so please run \"spin verman set custom --file path/to/spin/binary\" to proceed")
}

fmt.Println("Spin version custom found locally.")

} else {
if !strings.HasSuffix(pathToSpinBinary, "spin") {
return fmt.Errorf("the path specified is not to a file named \"spin\": %q", pathToSpinBinary)
}

// Make sure the Spin binary is not a directory
dirData, err := os.Stat(pathToSpinBinary)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("the file %q doesn't exist", pathToSpinBinary)
}
return err
}

// Check if the provided path is a file
if dirData.IsDir() {
return fmt.Errorf("the path %q exists, but is a directory, not a file", pathToSpinBinary)
}

// Creating a "custom" directory so that it shows up under the "list" command
symLinkPath := path.Join(versionDir, "custom", "spin")
symLinkPathExists, err := exists(symLinkPath)
if err != nil {
return err
}

if !symLinkPathExists {
if err = os.MkdirAll(symLinkPath, 0755); err != nil {
return err
}
}

// Removing old SymLink, returning an error only if the error is not a 'file does not exist' error
if err := os.Remove(path.Join(symLinkPath)); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to remove old symlink: %v", err)
}
}

if err := os.Symlink(path.Join(pathToSpinBinary), path.Join(symLinkPath)); err != nil {
return err
}

}

if err := updateSpinBinary(versionDir, "custom"); err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion spin-pluginify.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "verman"
version = "0.1.0"
version = "0.1.1"
# This version number is low because we need the ability to switch between different versions of Spin
spin_compatibility = ">=1.0"
license = "Apache-2.0"
Expand Down
Loading