github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/remote/remote.go (about)

     1  package remote
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/henvic/wedeploycli/color"
     9  	"github.com/henvic/wedeploycli/command/internal/we"
    10  	"github.com/henvic/wedeploycli/defaults"
    11  	"github.com/henvic/wedeploycli/formatter"
    12  	"github.com/henvic/wedeploycli/remotes"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  // RemoteCmd is the command to control Liferay Cloud remotes.
    17  var RemoteCmd = &cobra.Command{
    18  	Use:    "remote",
    19  	Hidden: true,
    20  	Short:  "Configure Liferay Cloud remotes",
    21  	Args:   cobra.NoArgs,
    22  	RunE:   remoteRun,
    23  }
    24  
    25  var setCmd = &cobra.Command{
    26  	Use:     "set",
    27  	Short:   "Set a remote named <name> with <url>",
    28  	Aliases: []string{"add"},
    29  	Args:    cobra.ExactArgs(2),
    30  	RunE:    setRun,
    31  }
    32  
    33  var renameCmd = &cobra.Command{
    34  	Use:    "rename",
    35  	Short:  "Rename the remote named <old> to <new>",
    36  	Args:   cobra.ExactArgs(2),
    37  	RunE:   renameRun,
    38  	Hidden: true,
    39  }
    40  
    41  var removeCmd = &cobra.Command{
    42  	Use:   "rm",
    43  	Short: "Remove the remote named <name>",
    44  	Args:  cobra.ExactArgs(1),
    45  	RunE:  removeRun,
    46  }
    47  
    48  var getURLCmd = &cobra.Command{
    49  	Use:    "get-url",
    50  	Short:  "Retrieves the URLs for a remote",
    51  	Args:   cobra.ExactArgs(1),
    52  	RunE:   getURLRun,
    53  	Hidden: true,
    54  }
    55  
    56  var setURLCmd = &cobra.Command{
    57  	Use:         "set-url",
    58  	Short:       "Changes URLs for the remote",
    59  	Args:        cobra.ExactArgs(2),
    60  	RunE:        setURLRun,
    61  	Hidden:      true,
    62  	Annotations: nil,
    63  }
    64  
    65  func remoteRun(cmd *cobra.Command, args []string) error {
    66  	var wectx = we.Context()
    67  	var conf = wectx.Config()
    68  	var params = conf.GetParams()
    69  	var rl = params.Remotes
    70  
    71  	var w = formatter.NewTabWriter(os.Stdout)
    72  
    73  	for _, k := range rl.Keys() {
    74  		var key = rl.Get(k)
    75  		var infrastructure = key.Infrastructure
    76  
    77  		_, _ = fmt.Fprintf(w, "%s\t%s", k, infrastructure)
    78  
    79  		if k == params.DefaultRemote {
    80  			_, _ = fmt.Fprintf(w, " (default)")
    81  		}
    82  
    83  		_, _ = fmt.Fprintf(w, "\n")
    84  	}
    85  
    86  	_ = w.Flush()
    87  
    88  	return nil
    89  }
    90  
    91  func setRun(cmd *cobra.Command, args []string) error {
    92  	var wectx = we.Context()
    93  	var conf = wectx.Config()
    94  	var params = conf.GetParams()
    95  	var r = params.Remotes
    96  
    97  	var name = args[0]
    98  
    99  	if r.Has(name) {
   100  		r.Del(name)
   101  	}
   102  
   103  	r.Set(name, remotes.Entry{
   104  		Infrastructure: args[1],
   105  	})
   106  
   107  	return conf.Save()
   108  }
   109  
   110  func renameRun(cmd *cobra.Command, args []string) error {
   111  	var wectx = we.Context()
   112  	var conf = wectx.Config()
   113  	var params = conf.GetParams()
   114  	var r = params.Remotes
   115  
   116  	var old = args[0]
   117  	var name = args[1]
   118  
   119  	if !r.Has(old) {
   120  		return errors.New("fatal: remote " + old + " does not exists.")
   121  	}
   122  
   123  	if r.Has(name) {
   124  		return errors.New("fatal: remote " + name + " already exists.")
   125  	}
   126  
   127  	var renamed = r.Get(old)
   128  
   129  	r.Del(old)
   130  	r.Set(name, renamed)
   131  	return conf.Save()
   132  }
   133  
   134  func removeRun(cmd *cobra.Command, args []string) error {
   135  	var wectx = we.Context()
   136  	var conf = wectx.Config()
   137  	var params = conf.GetParams()
   138  	var rl = params.Remotes
   139  
   140  	var name = args[0]
   141  
   142  	if !rl.Has(name) {
   143  		return errors.New("fatal: remote " + name + " does not exists.")
   144  	}
   145  
   146  	rl.Del(name)
   147  
   148  	if name == defaults.CloudRemote {
   149  		_, _ = fmt.Fprintf(os.Stderr, "%v\n", color.Format(color.FgHiRed, `Removed default cloud remote "lcp" will be recreated with its default value`))
   150  	}
   151  
   152  	if name == params.DefaultRemote && name != defaults.CloudRemote {
   153  		params.DefaultRemote = defaults.CloudRemote
   154  		conf.SetParams(params)
   155  		_, _ = fmt.Fprintf(os.Stderr, "%v\n", color.Format(color.FgHiRed, `Default remote reset to "lcp"`))
   156  	}
   157  
   158  	return conf.Save()
   159  }
   160  
   161  func getURLRun(cmd *cobra.Command, args []string) error {
   162  	var wectx = we.Context()
   163  	var conf = wectx.Config()
   164  	var params = conf.GetParams()
   165  	var rl = params.Remotes
   166  
   167  	var name = args[0]
   168  
   169  	if !rl.Has(name) {
   170  		return errors.New("fatal: remote " + name + " does not exists.")
   171  	}
   172  
   173  	var remote = rl.Get(name)
   174  
   175  	fmt.Println(remote.Infrastructure)
   176  	return nil
   177  }
   178  
   179  func setURLRun(cmd *cobra.Command, args []string) error {
   180  	var wectx = we.Context()
   181  	var conf = wectx.Config()
   182  	var params = conf.GetParams()
   183  	var rl = params.Remotes
   184  
   185  	var name = args[0]
   186  	var uri = args[1]
   187  
   188  	if !rl.Has(name) {
   189  		return errors.New("fatal: remote " + name + " does not exists.")
   190  	}
   191  
   192  	var remote = rl.Get(name)
   193  
   194  	remote.Infrastructure = uri
   195  
   196  	rl.Set(name, remote)
   197  
   198  	return conf.Save()
   199  }
   200  
   201  func init() {
   202  	RemoteCmd.AddCommand(setCmd)
   203  	RemoteCmd.AddCommand(renameCmd)
   204  	RemoteCmd.AddCommand(removeCmd)
   205  	RemoteCmd.AddCommand(getURLCmd)
   206  	RemoteCmd.AddCommand(setURLCmd)
   207  }