Skip to content

Commit 2714b30

Browse files
committed
ADD profile name validation, compress deliverables
1 parent 4331da7 commit 2714b30

File tree

8 files changed

+99
-10
lines changed

8 files changed

+99
-10
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ install:
99

1010
script:
1111
- go install ./...
12-
- gox -output="bin/{{.Dir}}_{{.OS}}_{{.Arch}}" ./...
12+
- gox -output="bin/{{.OS}}_{{.Arch}}/{{.Dir}}" ./...
1313

1414
before_deploy:
1515
- export VERSION=`npmrc version`
16+
- for i in bin/*; do tar -zvcf "$i.tar.gz" "$i"; done
1617

1718
deploy:
1819
provider: releases
1920
api_key: $GITHUB_TOKEN
2021
file_glob: true
21-
file: bin/**/*
22+
file: bin/*.tar.gz
2223
skip_cleanup: true
2324
name: npmrc $VERSION
2425
on:

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.1.2
2+
3+
- Added profile name validation
4+
- Added copy to current directory
5+
- Updated help and readme
6+
- Updated .travis.yml to compress files before uploading
7+
18
## 0.1.1
29

310
- Added remove confirmation

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ go install ./...
1515

1616
## Installation from releases
1717

18-
- Download the binary for your distribution
19-
- Rename it to `npmrc`
18+
- Download the file for your distribution
19+
- Extract it to get `npmrc`
2020
- Make it available in your path
2121

2222
Happy `.npmrc` switching!
@@ -43,5 +43,13 @@ version Display version
4343
| Env variable | Default value | Description |
4444
| --- | --- | --- |
4545
| NPMRC_DIR | `~` | Directory where profiles will be stored |
46-
| EDITOR | `vim` | Editor to use for creating/editing profiles |
46+
| EDITOR | `vi` | Editor to use for creating/editing profiles |
4747
| VIEWER | `cat` | Viewer to use for viewing profiles |
48+
49+
## Profile name rules
50+
51+
- Profile name must not start with `.`
52+
- Profile name must not contain spaces
53+
- Profile name must be of at least 1 character length
54+
55+
[Check regex at regexr.com](https://regexr.com/3u5rg)

cmd/npmrc/copy.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,29 @@ func CopyHandler(args []string, options CopyOptions) {
4343

4444
// Copy source profile to destination profile
4545
func Copy(srcProfile, destProfile string) error {
46+
var dest string
47+
4648
// check profile is valid
4749
if !ProfileExists(srcProfile) {
4850
fmt.Println("Error: Profile \"" + srcProfile + "\" does not exist")
4951
os.Exit(1)
5052
}
5153

54+
if destProfile == "." {
55+
dir, err := os.Getwd()
56+
if err != nil {
57+
log.Fatal(err)
58+
os.Exit(1)
59+
}
60+
61+
dest = path.Join(dir, NpmrcFile)
62+
} else {
63+
ValidateProfile(destProfile)
64+
dest = path.Join(Dir, NpmrcFile+"."+destProfile)
65+
}
66+
5267
// copy from $npmrc_dir/.npmrc.$srcProfile to $npmrc_dir/.npmrc.$destProfile
5368
source := path.Join(Dir, NpmrcFile+"."+srcProfile)
54-
dest := path.Join(Dir, NpmrcFile+"."+destProfile)
5569

5670
return CP(source, dest)
5771
}
@@ -66,5 +80,9 @@ Alias: cp
6680
Available flags:
6781
6882
verbose Display additional output
69-
h Display this message`)
83+
h Display this message
84+
85+
Details:
86+
87+
If <destProfile> is ".", the selected profile is copied to current working directory.`)
7088
}

cmd/npmrc/edit.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type EditOptions struct {
1818
func EditHandler(args []string, options EditOptions) {
1919
profile := args[0]
2020

21+
ValidateProfile(profile)
22+
2123
if options.verbose {
2224
if !ProfileExists(profile) {
2325
fmt.Println("Creating profile \"" + profile + "\"")

cmd/npmrc/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func main() {
136136
// Help display usage of npmrc command
137137
func Help() {
138138
fmt.Println(`
139-
Usage: npmrc <command> [flags]
139+
Usage: npmrc <command>
140140
141141
Available commands:
142142
@@ -147,5 +147,13 @@ edit, ed Create or update profiles
147147
copy, cp Copy profiles
148148
remove, rm Remove a profile
149149
help, h Display this message
150-
version Display version`)
150+
version Display version
151+
152+
Profile names:
153+
154+
A profile names must comply with the following:
155+
156+
- Must not start with "."
157+
- Must not contain spaces
158+
- Must be of at least 1 character length`)
151159
}

cmd/npmrc/validate.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"strings"
8+
)
9+
10+
// IsProfileValid checks if a given profile name complies with the naming rules
11+
func IsProfileValid(profile string) (bool, []error) {
12+
var errs []error
13+
14+
if strings.HasPrefix(profile, ".") {
15+
err := errors.New("Profile name cannot start with \".\"")
16+
17+
errs = append(errs, err)
18+
}
19+
20+
if strings.Contains(profile, " ") {
21+
err := errors.New("Profile name cannot contain spaces")
22+
23+
errs = append(errs, err)
24+
}
25+
26+
if len(profile) < 1 {
27+
err := errors.New("Profile name must be at least one character long")
28+
29+
errs = append(errs, err)
30+
}
31+
32+
return len(errs) == 0, errs
33+
}
34+
35+
// ValidateProfile aborts execution if profile is non compliant
36+
func ValidateProfile(profile string) {
37+
isValid, errors := IsProfileValid(profile)
38+
39+
if !isValid {
40+
for _, err := range errors {
41+
fmt.Println(err)
42+
}
43+
os.Exit(1)
44+
}
45+
}

cmd/npmrc/variables.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import homedir "github.com/mitchellh/go-homedir"
44

55
// NpmrcFile base name
66
const (
7-
Version = "0.1.1"
7+
Version = "0.1.2"
88
NpmrcFile = ".npmrc"
99
)
1010

0 commit comments

Comments
 (0)