github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/xray/cli.go (about)

     1  package xray
     2  
     3  import (
     4  	"errors"
     5  	"github.com/codegangsta/cli"
     6  	"github.com/jfrog/jfrog-cli-go/docs/common"
     7  	"github.com/jfrog/jfrog-cli-go/docs/xray/offlineupdate"
     8  	"github.com/jfrog/jfrog-cli-go/xray/commands"
     9  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    10  	"time"
    11  )
    12  
    13  const DATE_FORMAT = "2006-01-02"
    14  
    15  func GetCommands() []cli.Command {
    16  	return []cli.Command{
    17  		{
    18  			Name:         "offline-update",
    19  			Usage:        offlineupdate.Description,
    20  			HelpName:     common.CreateUsage("xr offline-update", offlineupdate.Description, offlineupdate.Usage),
    21  			ArgsUsage:    common.CreateEnvVars(),
    22  			Flags:        offlineUpdateFlags(),
    23  			Aliases:      []string{"ou"},
    24  			BashComplete: common.CreateBashCompletionFunc(),
    25  			Action:       offlineUpdates,
    26  		},
    27  	}
    28  }
    29  
    30  func offlineUpdateFlags() []cli.Flag {
    31  	return []cli.Flag{
    32  		cli.StringFlag{
    33  			Name:  "license-id",
    34  			Usage: "[Mandatory] Xray license ID` `",
    35  		},
    36  		cli.StringFlag{
    37  			Name:  "from",
    38  			Usage: "[Optional] From update date in YYYY-MM-DD format.` `",
    39  		},
    40  		cli.StringFlag{
    41  			Name:  "to",
    42  			Usage: "[Optional] To update date in YYYY-MM-DD format.` `",
    43  		},
    44  		cli.StringFlag{
    45  			Name:  "version",
    46  			Usage: "[Optional] Xray API version.` `",
    47  		},
    48  		cli.StringFlag{
    49  			Name:  "target",
    50  			Usage: "[Default: ./] Path for downloaded update files.` `",
    51  		},
    52  	}
    53  }
    54  
    55  func getOfflineUpdatesFlag(c *cli.Context) (flags *commands.OfflineUpdatesFlags, err error) {
    56  	flags = new(commands.OfflineUpdatesFlags)
    57  	flags.Version = c.String("version")
    58  	flags.License = c.String("license-id")
    59  	flags.Target = c.String("target")
    60  	if len(flags.License) < 1 {
    61  		return nil, errors.New("The --license-id option is mandatory.")
    62  	}
    63  	from := c.String("from")
    64  	to := c.String("to")
    65  	if len(to) > 0 && len(from) < 1 {
    66  		return nil, errors.New("The --from option is mandatory, when the --to option is sent.")
    67  	}
    68  	if len(from) > 0 && len(to) < 1 {
    69  		return nil, errors.New("The --to option is mandatory, when the --from option is sent.")
    70  	}
    71  	if len(from) > 0 && len(to) > 0 {
    72  		flags.From, err = dateToMilliseconds(from)
    73  		errorutils.CheckError(err)
    74  		if err != nil {
    75  			return
    76  		}
    77  		flags.To, err = dateToMilliseconds(to)
    78  		errorutils.CheckError(err)
    79  	}
    80  	return
    81  }
    82  
    83  func dateToMilliseconds(date string) (dateInMillisecond int64, err error) {
    84  	t, err := time.Parse(DATE_FORMAT, date)
    85  	if err != nil {
    86  		errorutils.CheckError(err)
    87  		return
    88  	}
    89  	dateInMillisecond = t.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
    90  	return
    91  }
    92  
    93  func offlineUpdates(c *cli.Context) error {
    94  	offlineUpdateFlags, err := getOfflineUpdatesFlag(c)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	return commands.OfflineUpdate(offlineUpdateFlags)
   100  }