github.com/pluralsh/plural-cli@v0.9.5/cmd/plural/link.go (about)

     1  package plural
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/pluralsh/plural-cli/pkg/manifest"
     7  	"github.com/urfave/cli"
     8  )
     9  
    10  func linkCommands() []cli.Command {
    11  	return []cli.Command{
    12  		{
    13  			Name:      "link",
    14  			Usage:     "links a local package into an installation repo",
    15  			ArgsUsage: "TOOL REPO",
    16  			Action:    handleLink,
    17  			Flags: []cli.Flag{
    18  				cli.StringFlag{
    19  					Name:  "name, n",
    20  					Usage: "the name of the artifact to link",
    21  				},
    22  				cli.StringFlag{
    23  					Name:  "path, f",
    24  					Usage: "local path to that artifact (can be relative)",
    25  				},
    26  			},
    27  		},
    28  		{
    29  			Name:      "unlink",
    30  			Usage:     "unlinks a linked package",
    31  			ArgsUsage: "REPO TOOL NAME",
    32  			Action:    handleUnlink,
    33  		},
    34  	}
    35  }
    36  
    37  func handleLink(c *cli.Context) error {
    38  	tool, repo := c.Args().Get(0), c.Args().Get(1)
    39  	name, path := c.String("name"), c.String("path")
    40  
    41  	if name == "" {
    42  		name = filepath.Base(path)
    43  	}
    44  
    45  	manPath, err := manifest.ManifestPath(repo)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	man, err := manifest.Read(manPath)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	man.AddLink(tool, name, path)
    56  
    57  	return man.Write(manPath)
    58  }
    59  
    60  func handleUnlink(c *cli.Context) error {
    61  	repo, tool := c.Args().Get(0), c.Args().Get(1)
    62  
    63  	manPath, err := manifest.ManifestPath(repo)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	man, err := manifest.Read(manPath)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	if tool == "all" {
    74  		man.UnlinkAll()
    75  	} else {
    76  		man.Unlink(tool, c.Args().Get(2))
    77  	}
    78  
    79  	return man.Write(manPath)
    80  }