Skip to content

Commit

Permalink
snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed May 6, 2020
1 parent cbf8d4a commit 8df98fe
Show file tree
Hide file tree
Showing 52 changed files with 4,858 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
os:
- macos-latest
- ubuntu-latest
- windows-latest
#- windows-latest # FIXME
runs-on: ${{ matrix.os }}
steps:
- name: Set up Go
Expand Down
45 changes: 2 additions & 43 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
linters:
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- gocritic
- godot
- gofmt
- goimports
- golint
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- prealloc
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
enable-all: true
disable:
- funlen
- gochecknoglobals
Expand All @@ -42,14 +10,11 @@ linters:
- gomnd
- lll
- maligned
- nakedret
- nestif
- testpackage
- wsl

linters-settings:
goimports:
local-prefixes: github.com/twpayne/chezmoi

issues:
exclude-rules:
- linters:
Expand All @@ -61,12 +26,6 @@ issues:
- linters:
- gochecknoinits
path: cmd/
- linters:
- gosec
path: internal/generate-assets/
- linters:
- gosec
path: internal/generate-helps/
- linters:
- scopelint
path: "_test\\.go"
8 changes: 8 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Add dir to RunScript?
Encryption should use System.IdempotentCommandOutput
Encryption
Test EncryptionTool that uses randomly generated key

script name is currently part of run once state (in key)
need to decide whether rename of script means its a different script
also executedAt in JSON representation should probably be runAt
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.2.2 // indirect
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/muesli/combinator v0.2.0
github.com/pelletier/go-toml v1.7.0
github.com/pkg/diff v0.0.0-20190930165518-531926345625
github.com/sergi/go-diff v1.1.0
Expand All @@ -44,6 +45,7 @@ require (
github.com/yuin/goldmark v1.1.28 // indirect
github.com/zalando/go-keyring v0.0.0-20200121091418-667557018717
go.etcd.io/bbolt v1.3.4
go.uber.org/multierr v1.5.0
golang.org/x/crypto v0.0.0-20200406173513-056763e48d71
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
Expand All @@ -52,3 +54,7 @@ require (
gopkg.in/ini.v1 v1.55.0 // indirect
gopkg.in/yaml.v2 v2.2.8
)

// Temporary until https://github.com/bmatcuk/doublestar/pull/34 and
// https://github.com/bmatcuk/doublestar/pull/35 are merged.
replace github.com/bmatcuk/doublestar => github.com/twpayne/doublestar v1.2.6
37 changes: 27 additions & 10 deletions go.sum

Large diffs are not rendered by default.

175 changes: 175 additions & 0 deletions v2/chezmoi/attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package chezmoi

import (
"strings"
)

// A SourceFileType is a source file type.
type SourceFileType int

// Source file types.
const (
SourceFileTypeFile SourceFileType = iota
SourceFileTypeScript
SourceFileTypeSymlink
)

// DirAttributes holds attributes parsed from a source directory name.
type DirAttributes struct {
Name string
Exact bool
Private bool
}

// A FileAttributes holds attributes parsed from a source file name.
type FileAttributes struct {
Name string
Type SourceFileType
Empty bool
Encrypted bool
Executable bool
Once bool
Private bool
Template bool
}

// ParseDirAttributes parses a single directory name.
func ParseDirAttributes(sourceName string) DirAttributes {
var (
name = sourceName
exact = false
private = false
)
if strings.HasPrefix(name, exactPrefix) {
name = strings.TrimPrefix(name, exactPrefix)
exact = true
}
if strings.HasPrefix(name, privatePrefix) {
name = strings.TrimPrefix(name, privatePrefix)
private = true
}
if strings.HasPrefix(name, dotPrefix) {
name = "." + strings.TrimPrefix(name, dotPrefix)
}
return DirAttributes{
Name: name,
Exact: exact,
Private: private,
}
}

// SourceName returns da's source name.
func (da DirAttributes) SourceName() string {
sourceName := ""
if da.Exact {
sourceName += exactPrefix
}
if da.Private {
sourceName += privatePrefix
}
if strings.HasPrefix(da.Name, ".") {
sourceName += dotPrefix + strings.TrimPrefix(da.Name, ".")
} else {
sourceName += da.Name
}
return sourceName
}

// ParseFileAttributes parses a source file name.
func ParseFileAttributes(sourceName string) FileAttributes {
var (
name = sourceName
typ = SourceFileTypeFile
empty = false
encrypted = false
executable = false
once = false
private = false
template = false
)
switch {
case strings.HasPrefix(name, runPrefix):
name = strings.TrimPrefix(name, runPrefix)
typ = SourceFileTypeScript
if strings.HasPrefix(name, oncePrefix) {
name = strings.TrimPrefix(name, oncePrefix)
once = true
}
case strings.HasPrefix(name, symlinkPrefix):
name = strings.TrimPrefix(name, symlinkPrefix)
typ = SourceFileTypeSymlink
if strings.HasPrefix(name, dotPrefix) {
name = "." + strings.TrimPrefix(name, dotPrefix)
}
default:
if strings.HasPrefix(name, encryptedPrefix) {
name = strings.TrimPrefix(name, encryptedPrefix)
encrypted = true
}
if strings.HasPrefix(name, privatePrefix) {
name = strings.TrimPrefix(name, privatePrefix)
private = true
}
if strings.HasPrefix(name, emptyPrefix) {
name = strings.TrimPrefix(name, emptyPrefix)
empty = true
}
if strings.HasPrefix(name, executablePrefix) {
name = strings.TrimPrefix(name, executablePrefix)
executable = true
}
if strings.HasPrefix(name, dotPrefix) {
name = "." + strings.TrimPrefix(name, dotPrefix)
}
}
if strings.HasSuffix(name, templateSuffix) {
name = strings.TrimSuffix(name, templateSuffix)
template = true
}
return FileAttributes{
Name: name,
Type: typ,
Empty: empty,
Encrypted: encrypted,
Executable: executable,
Once: once,
Private: private,
Template: template,
}
}

// SourceName returns fa's source name.
func (fa FileAttributes) SourceName() string {
sourceName := ""
switch fa.Type {
case SourceFileTypeFile:
if fa.Encrypted {
sourceName += encryptedPrefix
}
if fa.Private {
sourceName += privatePrefix
}
if fa.Empty {
sourceName += emptyPrefix
}
if fa.Executable {
sourceName += executablePrefix
}
case SourceFileTypeScript:
sourceName = runPrefix
if fa.Once {
sourceName += oncePrefix
}
case SourceFileTypeSymlink:
sourceName = symlinkPrefix
}
if strings.HasPrefix(fa.Name, ".") {
sourceName += dotPrefix + strings.TrimPrefix(fa.Name, ".")
} else {
sourceName += fa.Name
}
if fa.Template {
sourceName += templateSuffix
}
return sourceName
}
73 changes: 73 additions & 0 deletions v2/chezmoi/attributes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package chezmoi

import (
"testing"

"github.com/muesli/combinator"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDirAttributes(t *testing.T) {
testData := struct {
Name []string
Exact []bool
Private []bool
}{
Name: []string{"dir", ".dir"},
Exact: []bool{false, true},
Private: []bool{false, true},
}
var dirAttributes []DirAttributes
require.NoError(t, combinator.Generate(&dirAttributes, testData))
for _, da := range dirAttributes {
actualSourceName := da.SourceName()
actualDA := ParseDirAttributes(actualSourceName)
assert.Equal(t, da, actualDA)
assert.Equal(t, actualSourceName, actualDA.SourceName())
}
}

func TestFileAttributes(t *testing.T) {
var fileAttributes []FileAttributes
require.NoError(t, combinator.Generate(&fileAttributes, struct {
Type []SourceFileType
Name []string
Empty []bool
Encrypted []bool
Executable []bool
Private []bool
Template []bool
}{
Type: []SourceFileType{SourceFileTypeFile},
Name: []string{"name", ".name"},
Empty: []bool{false, true},
Encrypted: []bool{false, true},
Executable: []bool{false, true},
Private: []bool{false, true},
Template: []bool{false, true},
}))
require.NoError(t, combinator.Generate(&fileAttributes, struct {
Type []SourceFileType
Name []string
Once []bool
}{
Type: []SourceFileType{SourceFileTypeScript},
Name: []string{"name"},
Once: []bool{false, true},
}))
require.NoError(t, combinator.Generate(&fileAttributes, struct {
Type []SourceFileType
Name []string
Once []bool
}{
Type: []SourceFileType{SourceFileTypeSymlink},
Name: []string{".name", "name"},
}))
for _, fa := range fileAttributes {
actualSourceName := fa.SourceName()
actualFA := ParseFileAttributes(actualSourceName)
assert.Equal(t, fa, actualFA)
assert.Equal(t, actualSourceName, actualFA.SourceName())
}
}
Loading

0 comments on commit 8df98fe

Please sign in to comment.