Skip to content

Commit

Permalink
Add devcontaioner and related Dockerfile (#66)
Browse files Browse the repository at this point in the history
* Update: improvements to rootCommand and versionCmd

Signed-off-by: Santosh Kaluskar <[email protected]>

* chore: adds devcontainer and Dockerfile for the project

Signed-off-by: Santosh Kaluskar <[email protected]>

---------

Signed-off-by: Santosh Kaluskar <[email protected]>
  • Loading branch information
santoshkal authored Apr 30, 2024
1 parent c469263 commit a3030f0
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 36 deletions.
26 changes: 26 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ARG GO_VERSION=1.22
FROM golang:${GO_VERSION}-bullseye
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=1000
RUN apt-get update -y
RUN apt-get install python3 && ln -sf python3 /usr/bin/pyppthon
RUN apt-get install python3-pip -y \
&& apt-get install -y sudo \
vim \
make
RUN pip3 install --no-cache --upgrade pip setuptools
RUN pip install pre-commit

RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
&& echo "$USERNAME ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME

RUN apt install wget git make
RUN wget https://github.com/sigstore/gitsign/releases/download/v0.10.1/gitsign_0.10.1_linux_amd64 && \
chmod +x gitsign_0.10.1_linux_amd64 && \
mv ./gitsign_0.10.1_linux_amd64 /usr/bin/gitsign

# Setup shell
USER vscode
98 changes: 98 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/go
{
"name": "Go",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile

"build":{
"dockerfile": "Dockerfile",
"context": ".."
},
"containerEnv": {
"SHELL": "/bin/bash"
},

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {
// "ghcr.io/devcontainers-contrib/features/gitsign:1": {}
// },

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

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "go mod tidy && pre-commit install",

// Configure tool-specific properties.
"customizations": {
"settings": {
"eamodio.gitlens":["ui"],
"files.eol": "\n",
"remote.extensionKind": {
"ms-azuretools.vscode-docker": "workspace"
},
"editor.codeActionsOnSaveTimeout": 3000,
"go.useLanguageServer": true,
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
// Optional: Disable snippets, as they conflict with completion ranking.
// "editor.snippetSuggestions": "none"
},
"[go.mod]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
},
"gopls": {
"usePlaceholders": false,
"staticcheck": true
},
"go.autocompleteUnimportedPackages": true,
"go.gotoSymbol.includeImports": true,
"go.gotoSymbol.includeGoroot": true,
"go.lintTool": "golangci-lint",
"go.buildOnSave": "workspace",
"go.lintOnSave": "workspace",
"go.vetOnSave": "workspace",
"editor.formatOnSave": true,
"go.toolsEnvVars": {
"GOFLAGS": "-tags="
// "CGO_ENABLED": 1 // for the race detector
},
"gopls.env": {
"GOFLAGS": "-tags="
},
"go.testEnvVars": {
"": ""
},
"go.testFlags": ["-v", "-race"],
"go.testTimeout": "10s",
"go.coverOnSingleTest": true,
"go.coverOnSingleTestFile": true
},

// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"go.toolsManagement.checkForUpdates": "local",
"go.useLanguageServer": true,
"go.gopath": "/go"

},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"golang.Go",
"ms-azuretools.vscode-docker",
"eamodio.gitlens"
]
}
},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "vscode"
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Dockerfile*
genval
cosign
.todo

!.devcontainer/Dockerfile
2 changes: 1 addition & 1 deletion cmd/celval.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var celvalCmd = &cobra.Command{
Use: "celval",
Short: "celval commands manages to validation of Kubernetes and related manifests using Common Expression Language (CEL) policies",
Short: "celval commands manages validation of Kubernetes and related manifests using Common Expression Language (CEL) policies",
Long: `
celval command provides capabilities for validating configuration files for Kubernetes and related technolgies,
Terraform files, and Dockerfiles using Common Expression Language (CEL) policies.
Expand Down
2 changes: 1 addition & 1 deletion cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func init() {

var dockerfileCmd = &cobra.Command{
Use: "dockerfile",
Short: "Generate and Validate Dockerfile",
Short: "Generate and validate Dockerfile",
Long: `
A user can pass in a JSON file to genval, the passed input will be first evaluated based on input policies,
once input is validated, a Dockerfile will be generated and the generated Dockerfile will be further validated using
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

var cueCmd = &cobra.Command{
Use: "cue",
Short: "The cue command validates and generates Kubernetes manifests",
Short: "cue command validates and generates Kubernetes manifests",
Long: `
The Cue command enables the validation and generation of Kubernetes and related manifests.
Developers can provide an input manifest in either JSON or YAML format. This input is then validated
Expand Down
2 changes: 1 addition & 1 deletion cmd/cuemod.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var cuemodCmd = &cobra.Command{
Use: "cuemod",
Short: "cuemod commands manages workspace for interacting with Cue valoidation and generation",
Short: "cuemod command manages workspace for interacting with Cue valoidation and generation",
}

func init() {
Expand Down
40 changes: 16 additions & 24 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"

"github.com/fatih/color"
Expand All @@ -11,33 +10,26 @@ import (
// rootCommand returns a cobra command for genvalctl CLI tool
var rootCmd = &cobra.Command{
Use: "genval",
Short: "genval is a CLI tool to generate and validate files",
Short: "genval is a CLI tool to generate and validate configuration files",
Long: `
Genval is a versatile Go utility that simplifies configuration management by Generating and validating cobfig files
for a wide range of tools, including Dockerfile, Kubernetes manifests, Terraform files, Tekton, ArgoCD and more.
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(`
:'######:::'########:'##::: ##:'##::::'##::::'###::::'##:::::::
'##... ##:: ##.....:: ###:: ##: ##:::: ##:::'## ##::: ##:::::::
##:::..::: ##::::::: ####: ##: ##:::: ##::'##:. ##:: ##:::::::
##::'####: ######::: ## ## ##: ##:::: ##:'##:::. ##: ##:::::::
##::: ##:: ##...:::: ##. ####:. ##:: ##:: #########: ##:::::::
##::: ##:: ##::::::: ##:. ###::. ## ##::: ##.... ##: ##:::::::
. ######::: ########: ##::. ##:::. ###:::: ##:::: ##: ########:
:......::::........::..::::..:::::...:::::..:::::..::........::
Genval is a versatile Go utility that simplifies configuration management by Generating and validating cobfig files
for a wide range of tools, including Dockerfile, Kubernetes manifests, Terraform files, Tekton, ArgoCD and more.
`)
},
Genval is a versatile Go utility that simplifies configuration management by generating and validating configuration files
for a wide range of tools. It supports various file types, including Dockerfile, Kubernetes manifests,
custom resource definition (CRD) manifests, Terraform files, and more.
`,
}

func init() {
rootCmd.SetOut(color.Output)
rootCmd.SetErr(color.Error)
color.New(color.FgHiCyan, color.Bold).Println(`
:'######:::'########:'##::: ##:'##::::'##::::'###::::'##:::::::
'##... ##:: ##.....:: ###:: ##: ##:::: ##:::'## ##::: ##:::::::
##:::..::: ##::::::: ####: ##: ##:::: ##::'##:. ##:: ##:::::::
##::'####: ######::: ## ## ##: ##:::: ##:'##:::. ##: ##:::::::
##::: ##:: ##...:::: ##. ####:. ##:: ##:: #########: ##:::::::
##::: ##:: ##::::::: ##:. ###::. ## ##::: ##.... ##: ##:::::::
. ######::: ########: ##::. ##:::. ###:::: ##:::: ##: ########:
:......::::........::..::::..:::::...:::::..:::::..::........::
`)
}

func Execute() {
Expand Down
7 changes: 3 additions & 4 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package cmd

import (
"fmt"

"github.com/fatih/color"
"github.com/intelops/genval/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Genval",
Short: "Prints Genval version information",
Long: `All software has versions. This is Genval's`,
Run: func(cmd *cobra.Command, args []string) {
Version, err := utils.GetVersion()
if err != nil {
log.Errorf("Error fetching version info: %v", err)
}
fmt.Println(Version)
color.New(color.FgHiCyan, color.Bold).Println(Version)
},
}

Expand Down
16 changes: 13 additions & 3 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,20 @@ func StartSpinner(msg string) *spinner.Spinner {
}

func GetVersion() (string, error) {
cmd := exec.Command("git", "describe", "--tags", "--abbrev=0")
version, err := cmd.Output()
// Create the command to list all tag
cmdGitRevList := exec.Command("git", "rev-list", "--tags", "--max-count=1")
var out bytes.Buffer
cmdGitRevList.Stdout = &out
if err := cmdGitRevList.Run(); err != nil {
return "", fmt.Errorf("error running git rev-list: %v", err)
}
latestTag := strings.TrimSpace(out.String())

// Create the command to fetc the latest tag
cmdGitDescribe := exec.Command("git", "describe", "--tags", latestTag)
version, err := cmdGitDescribe.Output()
if err != nil {
return "", fmt.Errorf("error fetching git tags: %v", err)
return "", fmt.Errorf("error running git describe: %v", err)
}
return strings.TrimSpace("Genval: " + string(version)), nil
}

0 comments on commit a3030f0

Please sign in to comment.