Skip to content

Commit 38be661

Browse files
authored
Merge pull request #44 from stuartleeks/sl/snippets
Add initial (experimental) snippet support
2 parents b3719f8 + be04786 commit 38be661

File tree

17 files changed

+1414
-36
lines changed

17 files changed

+1414
-36
lines changed

.devcontainer/Dockerfile

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RUN apt-get update \
1313
&& apt-get -y install --no-install-recommends apt-utils dialog nano sudo bsdmainutils \
1414
#
1515
# Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
16-
&& apt-get -y install git iproute2 procps lsb-release \
16+
&& apt-get -y install git iproute2 procps lsb-release build-essential \
1717
# Install Release Tools
1818
#
1919
# --> RPM used by goreleaser
@@ -27,6 +27,17 @@ ARG USERNAME=vscode
2727
ARG USER_UID=1000
2828
ARG USER_GID=$USER_UID
2929

30+
# Create the user
31+
RUN groupadd --gid $USER_GID $USERNAME \
32+
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
33+
&& apt-get update \
34+
&& apt-get install -y sudo \
35+
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
36+
&& chmod 0440 /etc/sudoers.d/$USERNAME
37+
38+
# Set default user
39+
USER $USERNAME
40+
3041
# Set env for tracking that we're running in a devcontainer
3142
ENV DEVCONTAINER=true
3243

@@ -36,18 +47,19 @@ ENV GO111MODULE=on
3647
# Install Go tools
3748
RUN \
3849
# --> Delve for debugging
39-
go get github.com/go-delve/delve/cmd/dlv@v1.3.2 \
50+
go get github.com/go-delve/delve/cmd/dlv@v1.6.0 \
4051
# --> Go language server
41-
&& go get golang.org/x/tools/gopls@v0.2.1 \
52+
&& go get golang.org/x/tools/gopls@v0.6.10 \
4253
# --> Go symbols and outline for go to symbol support and test support
4354
&& go get github.com/acroca/[email protected] && go get github.com/ramya-rao-a/go-outline@7182a932836a71948db4a81991a494751eccfe77 \
4455
# --> GolangCI-lint
4556
&& curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sed 's/tar -/tar --no-same-owner -/g' | sh -s -- -b $(go env GOPATH)/bin \
4657
# --> Go releaser
47-
&& curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh -s -- "v0.164.0"\
58+
&& curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh -s -- "v0.164.0" \
4859
# --> Install junit converter
4960
&& go get github.com/jstemmer/[email protected] \
50-
&& rm -rf /go/src/ && rm -rf /go/pkg
61+
&& sudo rm -rf /go/src/ \
62+
&& sudo rm -rf /go/pkg
5163

5264
# Switch back to dialog for any ad-hoc use of apt-get
5365
ENV DEBIAN_FRONTEND=dialog

.devcontainer/devcontainer.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"usePlaceholders": true, // add parameter placeholders when completing a function
4141
// Experimental settings
4242
"completeUnimported": true, // autocomplete unimported packages
43-
"watchFileChanges": true, // watch file changes outside of the editor
4443
"deepCompletion": true, // enable deep completion
4544
},
4645
"files.eol": "\n", // formatting only supports LF line endings
@@ -49,15 +48,18 @@
4948
// Add the IDs of extensions you want installed when the container is created.
5049
"extensions": [
5150
"golang.go",
52-
"stuartleeks.vscode-go-by-example"
53-
]
51+
"stuartleeks.vscode-go-by-example",
52+
"darkriszty.markdown-table-prettify",
53+
"davidanson.vscode-markdownlint",
54+
"mushan.vscode-paste-image"
55+
],
5456

5557
// Use 'forwardPorts' to make a list of ports inside the container available locally.
5658
// "forwardPorts": [],
5759

5860
// Use 'postCreateCommand' to run commands after the container is created.
59-
// "postCreateCommand": "go version",
61+
"postCreateCommand": "sudo chown $(whoami) /go/pkg",
6062

6163
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
62-
// "remoteUser": "vscode"
64+
"remoteUser": "vscode"
6365
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ endif
3131

3232

3333
test:
34-
go test -v ./...
34+
richgo test -v ./...
3535

3636

3737
fmt:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ complete -F __start_devcontainer dc
3939

4040
## Docs
4141

42-
See [the documentation](https://stuartleeks.github.io/devcontainer-cli) on how to work with `devcontainer`.
42+
See [the documentation](https://stuartleeks.github.io/devcontainer-cli) on how to work with `devcontainer`.

cmd/devcontainer/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"github.com/spf13/cobra"
5+
"github.com/stuartleeks/devcontainer-cli/internal/pkg/config"
56
"github.com/stuartleeks/devcontainer-cli/internal/pkg/update"
67
)
78

@@ -27,6 +28,9 @@ func main() {
2728
rootCmd.AddCommand(createExecCommand())
2829
rootCmd.AddCommand(createListCommand())
2930
rootCmd.AddCommand(createTemplateCommand())
31+
if config.GetExperimentalFeaturesEnabled() {
32+
rootCmd.AddCommand(createSnippetCommand())
33+
}
3034
rootCmd.AddCommand(createUpdateCommand())
3135
rootCmd.AddCommand(createOpenInCodeCommand())
3236
rootCmd.AddCommand(createOpenInCodeInsidersCommand())

cmd/devcontainer/snippet.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"sort"
7+
"text/tabwriter"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/stuartleeks/devcontainer-cli/internal/pkg/devcontainers"
11+
)
12+
13+
func createSnippetCommand() *cobra.Command {
14+
cmd := &cobra.Command{
15+
Use: "snippet",
16+
Short: "work with snippets (experimental)",
17+
Long: "Use subcommands to work with devcontainer snippets (experimental)",
18+
}
19+
cmd.AddCommand(createSnippetListCommand())
20+
cmd.AddCommand(createSnippetAddCommand())
21+
return cmd
22+
}
23+
24+
func createSnippetListCommand() *cobra.Command {
25+
var listVerbose bool
26+
cmd := &cobra.Command{
27+
Use: "list",
28+
Short: "list snippets",
29+
Long: "List devcontainer snippets",
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
32+
snippets, err := devcontainers.GetSnippets()
33+
if err != nil {
34+
return err
35+
}
36+
37+
if listVerbose {
38+
w := new(tabwriter.Writer)
39+
// minwidth, tabwidth, padding, padchar, flags
40+
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
41+
defer w.Flush()
42+
43+
fmt.Fprintf(w, "%s\t%s\n", "SNIPPET NAME", "PATH")
44+
fmt.Fprintf(w, "%s\t%s\n", "-------------", "----")
45+
46+
for _, snippet := range snippets {
47+
fmt.Fprintf(w, "%s\t%s\n", snippet.Name, snippet.Path)
48+
}
49+
return nil
50+
}
51+
52+
for _, snippet := range snippets {
53+
fmt.Println(snippet.Name)
54+
}
55+
return nil
56+
},
57+
}
58+
cmd.Flags().BoolVarP(&listVerbose, "verbose", "v", false, "Verbose output")
59+
return cmd
60+
}
61+
62+
func createSnippetAddCommand() *cobra.Command {
63+
var devcontainerName string
64+
cmd := &cobra.Command{
65+
Use: "add SNIPPET_NAME",
66+
Short: "add snippet to devcontainer",
67+
Long: "Add a snippet to the devcontainer definition for the current folder",
68+
RunE: func(cmd *cobra.Command, args []string) error {
69+
70+
if len(args) != 1 {
71+
return cmd.Usage()
72+
}
73+
name := args[0]
74+
75+
currentDirectory, err := os.Getwd()
76+
if err != nil {
77+
return fmt.Errorf("Error reading current directory: %s\n", err)
78+
}
79+
80+
err = devcontainers.AddSnippetToDevcontainer(currentDirectory, name)
81+
if err != nil {
82+
return fmt.Errorf("Error setting devcontainer name: %s", err)
83+
}
84+
85+
return nil
86+
},
87+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
88+
// only completing the first arg (template name)
89+
if len(args) != 0 {
90+
return nil, cobra.ShellCompDirectiveNoFileComp
91+
}
92+
templates, err := devcontainers.GetSnippets()
93+
if err != nil {
94+
os.Exit(1)
95+
}
96+
names := []string{}
97+
for _, template := range templates {
98+
names = append(names, template.Name)
99+
}
100+
sort.Strings(names)
101+
return names, cobra.ShellCompDirectiveNoFileComp
102+
},
103+
}
104+
cmd.Flags().StringVar(&devcontainerName, "devcontainer-name", "", "Value to set the devcontainer.json name property to (default is folder name)")
105+
return cmd
106+
}

cmd/devcontainer/template.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"io/ioutil"
66
"os"
7-
"path/filepath"
87
"sort"
98
"text/tabwriter"
109

@@ -93,22 +92,10 @@ func createTemplateAddCommand() *cobra.Command {
9392
if err != nil {
9493
return fmt.Errorf("Error reading current directory: %s\n", err)
9594
}
96-
if err = ioutil2.CopyFolder(template.Path, currentDirectory+"/.devcontainer"); err != nil {
97-
return fmt.Errorf("Error copying folder: %s\n", err)
98-
}
9995

100-
// by default the "name" in devcontainer.json is set to the name of the template
101-
// override it here with the value passed in as --devcontainer-name (or the containing folder if not set)
102-
if devcontainerName == "" {
103-
devcontainerName, err = devcontainers.GetDefaultDevcontainerNameForFolder(currentDirectory)
104-
if err != nil {
105-
return fmt.Errorf("Error getting default devcontainer name: %s", err)
106-
}
107-
}
108-
devcontainerJsonPath := filepath.Join(currentDirectory, ".devcontainer/devcontainer.json")
109-
err = devcontainers.SetDevcontainerName(devcontainerJsonPath, devcontainerName)
96+
err = devcontainers.CopyTemplateToFolder(template.Path, currentDirectory, devcontainerName)
11097
if err != nil {
111-
return fmt.Errorf("Error setting devcontainer name: %s", err)
98+
return err
11299
}
113100

114101
return nil

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ See the following topics for more information:
2828
* [open-in-code](open-in-code) - open dev containers in VS Code from the terminal
2929
* [template](template) - add dev container definitions to a folder
3030
* [exec](exec) - launch a terminal or other command in a dev container
31-
31+
* [snippet](snippet) - add snippets to an existing dev container definition **experimental**

0 commit comments

Comments
 (0)