github.com/hattya/nazuna@v0.7.1-0.20240331055452-55e14c275c1c/cmd/nzn/link.go (about) 1 // 2 // nazuna/cmd/nzn :: link.go 3 // 4 // Copyright (c) 2013-2020 Akinori Hattori <hattya@gmail.com> 5 // 6 // SPDX-License-Identifier: MIT 7 // 8 9 package main 10 11 import ( 12 "path/filepath" 13 "strings" 14 15 "github.com/hattya/go.cli" 16 "github.com/hattya/nazuna" 17 ) 18 19 func init() { 20 flags := cli.NewFlagSet() 21 flags.String("l, layer", "", "layer name") 22 flags.String("p, path", "", "list of directories to search <src>") 23 24 app.Add(&cli.Command{ 25 Name: []string{"link"}, 26 Usage: "-l <layer> [-p <path>] <src> <dst>", 27 Desc: strings.TrimSpace(cli.Dedent(` 28 create a link for the specified path 29 30 link is used to create a link of <src> to <dst>, and will be managed by 31 update. If <src> is not found on update, it will be ignored without error. 32 33 The value of --path flag is a list of directories like PATH or GOPATH 34 environment variables, and it is used to search <src>. 35 36 You can refer environment variables in <path> and <src>. Supported formats 37 are ${var} and $var. 38 `)), 39 Flags: flags, 40 Action: link, 41 Data: true, 42 }) 43 } 44 45 func link(ctx *cli.Context) error { 46 repo := ctx.Data.(*nazuna.Repository) 47 wc, err := repo.WC() 48 if err != nil { 49 return err 50 } 51 52 switch { 53 case ctx.String("layer") == "": 54 return cli.FlagError("--layer flag is required") 55 default: 56 if len(ctx.Args) != 2 { 57 return cli.ErrArgs 58 } 59 l, err := repo.LayerOf(ctx.String("layer")) 60 if err != nil { 61 return err 62 } 63 dst, err := wc.Rel('.', ctx.Args[1]) 64 if err != nil { 65 return err 66 } 67 if _, err = l.NewLink(filepath.SplitList(ctx.String("path")), ctx.Args[0], dst); err != nil { 68 return err 69 } 70 } 71 return repo.Flush() 72 }