-
Notifications
You must be signed in to change notification settings - Fork 9
/
workspace.go
193 lines (165 loc) · 5.29 KB
/
workspace.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package create
import (
"context"
"fmt"
"io"
"os"
"strings"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/console/tui"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/git"
"namespacelabs.dev/foundation/internal/workspace"
)
const (
workspaceFileTemplate = `module: "%s"
`
vscodeExtensionsFilePath = ".vscode/extensions.json"
vscodeExtensionsTemplate = `{
"recommendations": [
"golang.go",
"esbenp.prettier-vscode",
"zxh404.vscode-proto3",
"namespacelabs.namespace-vscode"
]
}`
gitignoreFilePath = ".gitignore"
gitignoreTemplate = `# Namespace configuration of this specific host.
devhost.textpb
# Typescript/Node.js/Yarn
node_modules
**/.yarn/*
!**/.yarn/patches
!**/.yarn/plugins
!**/.yarn/releases
!**/.yarn/sdks
!**/.yarn/versions
`
gitpodFilePath = ".gitpod.yml"
// `ns create starter --dry_run` prints to the terminal in VSCode what we have done for the user.
//
// Use the pinned ns for testing/development, e.g. "image: us-docker.pkg.dev/foundation-344819/prebuilts/namespacelabs.dev/foundation/internal/gitpod/pinned@sha256:<SHA>"
gitpodTemplate = `image: us-docker.pkg.dev/foundation-344819/prebuilts/namespacelabs.dev/foundation/internal/gitpod/nsboot@sha256:b8b169a9c5a8074e26e4f6949ab56b060cfed7af31b5c7f073199ad3cea88c89
tasks:
- name: prepare
before: |
ns login --read_from_env_var=NS_AUTH
init: |
gp env NS_AUTH=` + "`" + `cat ~/.config/fn/auth.json` + "`" + ` &> /dev/null
ns prepare new-cluster
ns create starter --dry_run --suggest_prepare=false
ports:
- name: Namespace Dev UI
port: 4001
onOpen: open-preview
`
)
func newWorkspaceCmd(runCommand func(ctx context.Context, args []string) error) *cobra.Command {
cmd := &cobra.Command{
Use: "workspace [directory]",
Short: "Initializes a workspace.",
}
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
fsfs := fnfs.ReadWriteLocalFS(cwd)
if err := writeWorkspaceConfig(ctx, fsfs, args); err != nil {
return err
}
if err := writeFileIfDoesntExist(ctx, console.Stdout(ctx), fsfs, vscodeExtensionsFilePath, vscodeExtensionsTemplate); err != nil {
return err
}
if err := updateGitignore(ctx, console.Stdout(ctx), fsfs); err != nil {
return err
}
if isRoot, err := git.IsRepoRoot(ctx, "."); err == nil && isRoot {
if err := writeFileIfDoesntExist(ctx, console.Stdout(ctx), fsfs, gitpodFilePath, gitpodTemplate); err != nil {
return err
}
}
return runCommand(ctx, []string{"tidy"})
})
return cmd
}
func askWorkspaceName(ctx context.Context) (string, error) {
placeholder := "github.com/username/reponame"
if url, err := git.RemoteUrl(ctx, "."); err == nil {
placeholder = url
}
return tui.Ask(ctx,
"Workspace name?",
"The workspace name should to match the Github repository name.",
placeholder)
}
func workspaceNameFromArgs(ctx context.Context, args []string) (string, error) {
if len(args) == 0 {
workspaceName, err := askWorkspaceName(ctx)
if err != nil {
return "", err
}
if workspaceName == "" {
return "", context.Canceled
}
return workspaceName, nil
} else {
return args[0], nil
}
}
func writeWorkspaceConfig(ctx context.Context, fsfs fnfs.ReadWriteFS, args []string) error {
workspaceName, err := workspaceNameFromArgs(ctx, args)
if err != nil {
return err
}
if workspaceName == "" {
return context.Canceled
}
return writeFileIfDoesntExist(ctx, nil, fsfs, workspace.WorkspaceFile, fmt.Sprintf(workspaceFileTemplate, workspaceName))
}
func writeFileIfDoesntExist(ctx context.Context, out io.Writer, fsfs fnfs.ReadWriteFS, fn string, content string) error {
if f, err := fsfs.Open(fn); err == nil {
f.Close()
return nil
}
return fnfs.WriteWorkspaceFile(ctx, out, fsfs, fn, func(w io.Writer) error {
_, err := fmt.Fprint(w, content)
return err
})
}
func updateGitignore(ctx context.Context, out io.Writer, fsfs fnfs.ReadWriteFS) error {
f, err := fsfs.Open(gitignoreFilePath)
if err != nil {
// file does not exist
return fnfs.WriteWorkspaceFile(ctx, out, fsfs, gitignoreFilePath, func(w io.Writer) error {
_, err := fmt.Fprint(w, gitignoreTemplate)
return err
})
}
defer f.Close()
data, err := io.ReadAll(f)
if err != nil {
return err
}
content := string(data)
if strings.Contains(content, "devhost.textpb") {
// Found Namespace bits in .gitignore. Don't touch it.
return nil
}
if strings.Contains(content, "yarn") {
// .gitignore is apparently not generated by us but already has some yarn bits.
// Let's not touch it but just say what we'd recommend.
fmt.Fprintf(console.Stdout(ctx), "Found existing .gitignore file. Consider adding:\n# Namespace .gitignore begin\n%s\n# Namespace .gitignore end\n", gitignoreTemplate)
return nil
}
content = fmt.Sprintf("%s\n%s", content, gitignoreTemplate)
return fnfs.WriteWorkspaceFile(ctx, out, fsfs, gitignoreFilePath, func(w io.Writer) error {
_, err := fmt.Fprint(w, content)
return err
})
}