Skip to content

Commit

Permalink
snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Apr 22, 2020
1 parent d2d43e9 commit 03e378e
Show file tree
Hide file tree
Showing 47 changed files with 4,498 additions and 3 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
7 changes: 7 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Add dir to RunScript?
Encryption
Templates

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
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bmatcuk/doublestar v1.2.4 h1:CXTEjc5/WPKLJEqrS9D0IQAUhpjAIJuUQ4XtXG+zmEU=
github.com/bmatcuk/doublestar v1.2.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/charmbracelet/glamour v0.1.0 h1:BHCtc+YJjoBjNUnFKBtXyyM4Bp9u7L2kf49qV+/AGYw=
github.com/charmbracelet/glamour v0.1.0/go.mod h1:Z1C2JkVGBom/RYfoKcPBZ81lHMR3xp3W6OCLNWWEIMc=
Expand Down Expand Up @@ -265,6 +263,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/twpayne/doublestar v1.2.6 h1:nkd+GJXRKQf8HxOZQ+G7Qtu9mwRNLc5so/oXgRcyWD8=
github.com/twpayne/doublestar v1.2.6/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/twpayne/go-shell v0.1.1 h1:Kr1hSEFrPBTRmhOW8woaj7ZxV3/OH7Qefg86OFJ5DFc=
github.com/twpayne/go-shell v0.1.1/go.mod h1:H/gzux0DOH5jsjQSHXs6rs2Onxy+V4j6ycZTOulC0l8=
github.com/twpayne/go-vfs v1.0.1/go.mod h1:OIXA6zWkcn7Jk46XT7ceYqBMeIkfzJ8WOBhGJM0W4y8=
Expand Down
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
}
Loading

0 comments on commit 03e378e

Please sign in to comment.