-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathlinks.go
146 lines (124 loc) · 3.95 KB
/
links.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/elastic/elastic-package/internal/cobraext"
"github.com/elastic/elastic-package/internal/files"
)
const (
linksLongDescription = `Use this command to manage linked files in the repository.`
linksCheckLongDescription = `Use this command to check if linked files references inside the current directory are up to date.`
linksUpdateLongDescription = `Use this command to update all linked files references inside the current directory.`
linksListLongDescription = `Use this command to list all packages that have linked file references that include the current directory.`
)
func setupLinksCommand() *cobraext.Command {
cmd := &cobra.Command{
Use: "links",
Short: "Manage linked files",
Long: linksLongDescription,
RunE: func(parent *cobra.Command, args []string) error {
return cobraext.ComposeCommandsParentContext(parent, args, parent.Commands()...)
},
}
cmd.AddCommand(getLinksCheckCommand())
cmd.AddCommand(getLinksUpdateCommand())
cmd.AddCommand(getLinksListCommand())
return cobraext.NewCommand(cmd, cobraext.ContextGlobal)
}
func getLinksCheckCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "check",
Short: "Check for linked files changes",
Long: linksCheckLongDescription,
Args: cobra.NoArgs,
RunE: linksCheckCommandAction,
}
return cmd
}
func linksCheckCommandAction(cmd *cobra.Command, args []string) error {
cmd.Printf("Check for linked files changes\n")
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("reading current working directory failed: %w", err)
}
linkedFiles, err := files.AreLinkedFilesUpToDate(pwd)
if err != nil {
return fmt.Errorf("checking linked files are up-to-date failed: %w", err)
}
for _, f := range linkedFiles {
if !f.UpToDate {
cmd.Printf("%s is outdated.\n", filepath.Join(f.WorkDir, f.LinkFilePath))
}
}
if len(linkedFiles) > 0 {
return fmt.Errorf("linked files are outdated")
}
return nil
}
func getLinksUpdateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "Update linked files checksums if needed.",
Long: linksUpdateLongDescription,
Args: cobra.NoArgs,
RunE: linksUpdateCommandAction,
}
return cmd
}
func linksUpdateCommandAction(cmd *cobra.Command, args []string) error {
cmd.Printf("Update linked files checksums if needed.\n")
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("reading current working directory failed: %w", err)
}
updatedLinks, err := files.UpdateLinkedFilesChecksums(pwd)
if err != nil {
return fmt.Errorf("updating linked files checksums failed: %w", err)
}
for _, l := range updatedLinks {
cmd.Printf("%s was updated.\n", l.LinkFilePath)
}
return nil
}
func getLinksListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List packages linking files from this path",
Long: linksListLongDescription,
Args: cobra.NoArgs,
RunE: linksListCommandAction,
}
cmd.Flags().BoolP(cobraext.PackagesFlagName, "", false, cobraext.PackagesFlagDescription)
return cmd
}
func linksListCommandAction(cmd *cobra.Command, args []string) error {
onlyPackages, err := cmd.Flags().GetBool(cobraext.PackagesFlagName)
if err != nil {
return cobraext.FlagParsingError(err, cobraext.PackagesFlagName)
}
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("reading current working directory failed: %w", err)
}
byPackage, err := files.LinkedFilesByPackageFrom(pwd)
if err != nil {
return fmt.Errorf("listing linked packages failed: %w", err)
}
for i := range byPackage {
for p, links := range byPackage[i] {
if onlyPackages {
cmd.Println(p)
continue
}
for _, l := range links {
cmd.Println(l)
}
}
}
return nil
}