Run runs the command logic
(_ *cobra.Command, _ []string)
| 55 | |
| 56 | // Run runs the command logic |
| 57 | func (cmd *ConfigureCmd) Run(_ *cobra.Command, _ []string) error { |
| 58 | // try to load the old commands |
| 59 | oldCommands := []string{} |
| 60 | out, err := os.ReadFile(proxyCommandsPath) |
| 61 | if err == nil { |
| 62 | oldCommands = strings.Split(string(out), ",") |
| 63 | } |
| 64 | |
| 65 | // first configure the commands |
| 66 | for _, c := range cmd.Commands { |
| 67 | fileDir := "/usr/local/bin/" |
| 68 | filePath := fileDir + c |
| 69 | |
| 70 | _, err := os.Stat(fileDir) |
| 71 | if err != nil { |
| 72 | if !os.IsNotExist(err) { |
| 73 | return fmt.Errorf("error checking for command path '%s': %v", fileDir, err) |
| 74 | } |
| 75 | |
| 76 | err := os.MkdirAll(fileDir, 0755) |
| 77 | if err != nil { |
| 78 | return fmt.Errorf("error creating command path '%s': %v", fileDir, err) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | executeCommand := fmt.Sprintf(`#!/bin/sh |
| 83 | /tmp/devspacehelper proxy-commands run %s "$@"`, c) |
| 84 | err = os.WriteFile(filePath, []byte(executeCommand), 0777) |
| 85 | if err != nil { |
| 86 | return fmt.Errorf("error writing command '%s': %v", filePath, err) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // remove commands that are not there anymore |
| 91 | for _, oldCommand := range oldCommands { |
| 92 | found := false |
| 93 | for _, c := range cmd.Commands { |
| 94 | if oldCommand == c { |
| 95 | found = true |
| 96 | break |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if !found { |
| 101 | _ = os.Remove("/usr/local/bin/" + oldCommand) |
| 102 | } |
| 103 | } |
| 104 | err = os.WriteFile(proxyCommandsPath, []byte(strings.Join(cmd.Commands, ",")), 0644) |
| 105 | if err != nil { |
| 106 | stderrlog.Errorf("error writing %s: %v", proxyCommandsPath, err) |
| 107 | } |
| 108 | |
| 109 | // now configure the ssh config |
| 110 | if cmd.PublicKey != "" && cmd.PrivateKey != "" { |
| 111 | // decode public key |
| 112 | decodedPublicKey, err := base64.StdEncoding.DecodeString(cmd.PublicKey) |
| 113 | if err != nil { |
| 114 | return errors.Wrap(err, "decode public key") |