Skip to content

Commit

Permalink
Adding addProvider command, and devcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
PrashamTrivedi committed Jul 17, 2021
1 parent 67a08dc commit 5fd535f
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.187.0/containers/go/.devcontainer/base.Dockerfile

# [Choice] Go version: 1, 1.16, 1.15
ARG VARIANT="1.16"
FROM mcr.microsoft.com/vscode/devcontainers/go:0-${VARIANT}

# [Option] Install Node.js
ARG INSTALL_NODE="true"
ARG NODE_VERSION="lts/*"
RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment the next line to use go get to install anything else you need
# RUN go get -u github.com/spf13/cobra

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
38 changes: 38 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.187.0/containers/go
{
"name": "Go",
"build": {
"dockerfile": "Dockerfile",
"args": {
// Update the VARIANT arg to pick a version of Go: 1, 1.16, 1.15
"VARIANT": "1.16",
// Options
"INSTALL_NODE": "true",
"NODE_VERSION": "lts/*"
}
},
"runArgs": [
"--cap-add=SYS_PTRACE",
"--security-opt",
"seccomp=unconfined"
],
// Set *default* container specific settings.json values on container create.
"settings": {
"go.toolsManagement.checkForUpdates": "local",
"go.useLanguageServer": true,
"go.gopath": "/go",
"go.goroot": "/usr/local/go"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"golang.Go",
"GitHub.copilot"
],
// 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 version",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
26 changes: 26 additions & 0 deletions cmd/addProvider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"github.com/spf13/cobra"
)

//create two string variables to hold providerName and providerURL
var providerName, providerURL string

//create cobra command
var addProviderCmd = &cobra.Command{
Use: "addProvider",
Short: "Adds a git provider for you",
Long: `This command will add a git provider for you`,
Run: func(cmd *cobra.Command, args []string) {
//call add provider with the providerName and providerURL keep properties blank
AddProvider(providerName, providerURL)
},
}

func init() {
addProviderCmd.Flags().StringVarP(&providerName, "name", "n", "", "Name of the provider")
addProviderCmd.Flags().StringVarP(&providerURL, "url", "u", "", "URL of the provider")

RootCmd.AddCommand(addProviderCmd)
}
48 changes: 46 additions & 2 deletions cmd/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,32 @@ var defaultProviders []GitProvider = []GitProvider{

var currentProviders []GitProvider

//Check if the provider is already in the list, if not, add into the list and save to the file.
func AddProvider(providerName string, providerUrl string) {
provider, index := GetProviderByName(providerName)
//if index is -1 get provider by url
if index == -1 {
provider, index = GetProviderByUrl(providerUrl)
}
//return provider if index is not -1
if index != -1 {
currentProviders[index] = provider
return
} else {
//create a git provider with providerName and providerUrl, keep properties blank
provider := GitProvider{Name: providerName, Url: providerUrl, Properties: make(map[string]string)}
currentProviders = append(currentProviders, provider)
writeGitProviders(currentProviders)
}
}

func AddProviderPropertyFromName(providerName string, propertyKey string, propertyValue string) {
provider, index := GetProviderByName(providerName)
//throw error if index is -1
if index == -1 {
fmt.Println("Error: Provider not found")
os.Exit(1)
}
if provider.Properties == nil || len(provider.Properties) == 0 {
provider.Properties = make(map[string]string)
}
Expand Down Expand Up @@ -100,6 +124,13 @@ func runUpdateCommand(key string, value string) {
}
func AddProviderPropertyFromUrl(providerUrl string, propertyKey string, propertyValue string) {
provider, index := GetProviderByUrl(providerUrl)

//throw error if index is -1
if index == -1 {
fmt.Println("Error: Provider not found")
os.Exit(1)
}

if provider.Properties == nil || len(provider.Properties) == 0 {
provider.Properties = make(map[string]string)
}
Expand All @@ -110,13 +141,21 @@ func AddProviderPropertyFromUrl(providerUrl string, propertyKey string, property

func RemoveProviderProperty(providerName string, propertyKey string) {
provider, index := GetProviderByName(providerName)

//throw error if index is -1
if index == -1 {
fmt.Println("Error: Provider not found")
os.Exit(1)
}

if provider.Properties == nil || len(provider.Properties) == 0 {
provider.Properties = make(map[string]string)
}
delete(provider.Properties, propertyKey)
updateProvider(provider, index)
runUpdateCommand(propertyKey, "")
}

func GetProviderByName(providerName string) (GitProvider, int) {
indexToReturn := -1
var providerToReturn GitProvider
Expand All @@ -141,7 +180,7 @@ func GetProviderByUrl(providerUrl string) (GitProvider, int) {
readGitProviders()
}
for index, provider := range currentProviders {
if strings.HasPrefix(provider.Url, providerUrl) {
if strings.HasPrefix(providerUrl, provider.Url) {
providerToReturn = provider
indexToReturn = index
break
Expand Down Expand Up @@ -173,7 +212,12 @@ func ApplyPropertiesForRemote() {
}
}

provider, _ := GetProviderByUrl(firstRemote.Url)
provider, index := GetProviderByUrl(firstRemote.Url)
//if index is -1 then no provider found
if index == -1 {
fmt.Println("Error: Provider not found")
os.Exit(1)
}
applyCommandFromProvider(provider)

}
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ These are list of commands along with their use cases. To know the inputs and ot

- `listProviders`: Lists all providers along with properties we have added using this CLI,

- `addProvider`: Adds new provider with empty properties

- `addConfig`: Adds properties as key value pair to given git provider.

- `deleteConfig`: Deletes property from given git provider.
Expand Down

0 comments on commit 5fd535f

Please sign in to comment.