forked from crossplane/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.go
More file actions
140 lines (114 loc) · 4.29 KB
/
Copy pathinit.go
File metadata and controls
140 lines (114 loc) · 4.29 KB
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
/*
Copyright 2026 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package project
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/google/go-containerregistry/pkg/name"
"k8s.io/apimachinery/pkg/util/validation"
"github.com/crossplane/crossplane-runtime/v2/pkg/errors"
"github.com/crossplane/cli/v2/internal/terminal"
_ "embed"
)
//go:embed help/init.md
var initHelp string
const projectFileName = "crossplane-project.yaml"
// initCmd initializes a new project.
type initCmd struct {
Name string `arg:"" help:"The name of the new project."`
Directory string `help:"Directory to initialize. Defaults to project name" short:"d" type:"path"`
Registry string `default:"example.com/my-org" help:"Override the registry in the project file." optional:"" short:"r"`
Repository string `help:"Override the repository name in the project file. Defaults to the project name." optional:""`
}
func (c *initCmd) Help() string {
return initHelp
}
func (c *initCmd) Run(sp terminal.SpinnerPrinter) error {
// Validate the project name is a valid DNS-1035 label.
if errs := validation.IsDNS1035Label(c.Name); len(errs) > 0 {
return errors.Errorf("'%s' is not a valid project name. DNS-1035 constraints: %s", c.Name, strings.Join(errs, "; "))
}
if c.Directory == "" {
c.Directory = c.Name
}
if strings.TrimSpace(c.Repository) == "" {
c.Repository = c.Name
}
// Check if the target directory is suitable.
if err := c.checkTargetDirectory(); err != nil {
return err
}
rp := strings.TrimRight(strings.TrimSpace(c.Registry), "/")
if rp == "" {
return errors.New("registry cannot be empty; set --registry to an OCI registry prefix like 'xpkg.crossplane.io/my-org'")
}
r, err := name.NewRepository(rp + "/" + c.Repository)
if err != nil {
return errors.Wrapf(err, "cannot build repository \"%s/%s\"", rp, c.Repository)
}
return sp.WrapWithSuccessSpinner("Initializing project", func() error {
if err := os.MkdirAll(c.Directory, 0o750); err != nil {
return errors.Wrapf(err, "failed to create directory %s", c.Directory)
}
// Write a minimal crossplane-project.yaml.
projFile := filepath.Join(c.Directory, projectFileName)
content := fmt.Sprintf(`apiVersion: dev.crossplane.io/v1alpha1
kind: Project
metadata:
name: %s
spec:
repository: %s
`, c.Name, r.String())
if err := os.WriteFile(projFile, []byte(content), 0o600); err != nil {
return errors.Wrapf(err, "failed to write %s", projectFileName)
}
// Create default subdirectories.
dirs := []string{"apis", "functions", "examples", "tests", "operations"}
for _, dir := range dirs {
dirPath := filepath.Join(c.Directory, dir)
if err := os.MkdirAll(dirPath, 0o700); err != nil {
return errors.Wrapf(err, "failed to create directory %s", dirPath)
}
// Write a .gitkeep so empty dirs are tracked.
keepFile := filepath.Join(dirPath, ".gitkeep")
if err := os.WriteFile(keepFile, nil, 0o600); err != nil {
return errors.Wrapf(err, "failed to write %s", keepFile)
}
}
return nil
})
}
func (c *initCmd) checkTargetDirectory() error {
f, err := os.Stat(c.Directory)
switch {
case os.IsNotExist(err):
return nil // Will be created
case err != nil:
return errors.Wrapf(err, "failed to stat directory %s", c.Directory)
case !f.IsDir():
return errors.Errorf("path %s is not a directory", c.Directory)
}
entries, err := os.ReadDir(c.Directory)
if err != nil {
return errors.Wrapf(err, "failed to read directory %s", c.Directory)
}
for _, entry := range entries {
if entry.Name() == ".git" && entry.IsDir() {
continue
}
return errors.Errorf("directory %s is not empty", c.Directory)
}
return nil
}