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

cmd: add 'remote import' #703

Merged
merged 1 commit into from
Oct 18, 2020
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
40 changes: 40 additions & 0 deletions cmd/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package remotecmd
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"

"github.com/BurntSushi/toml"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"

Expand Down Expand Up @@ -65,6 +67,7 @@ For example:
remote.attachListCmd()
remote.attachRemoveCmd()
remote.attachUpgradeCmd()
remote.attachImportCmd()
remote.attachResetCmd()
remote.attachConfigPathCmd()

Expand Down Expand Up @@ -450,6 +453,43 @@ func (root *RemoteCmd) attachSetCmd() {
root.AddCommand(set)
}

func (root *RemoteCmd) attachImportCmd() {
var importCmd = &cobra.Command{
Use: "import [file] [remote]",
Short: "Import remote from a remote configuration file",
Long: `Import remote from another remote configuration file into your global
remotes configuration.`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
var (
importPath = args[0]
remoteName = args[1]
)

// load provided config
raw, err := ioutil.ReadFile(importPath)
if err != nil {
out.Fatal(err)
}
var remotes cfg.Remotes
if err = toml.Unmarshal(raw, &remotes); err != nil {
out.Fatalf("failed to read configuration file %q: %v", importPath, err)
}
remoteCfg, found := remotes.GetRemote(remoteName)
if !found {
out.Fatalf("no remote %q found in %q", remoteName, importPath)
}

// import provided config
if err := local.SaveRemote(remoteCfg); err != nil {
out.Fatal(err)
}
out.Printf("imported remote %q from %q\n", remoteName, importPath)
},
}
root.AddCommand(importCmd)
}

func (root *RemoteCmd) attachResetCmd() {
var resetCmd = &cobra.Command{
Use: "reset",
Expand Down