Skip to content

Commit

Permalink
create .lets when config found
Browse files Browse the repository at this point in the history
  • Loading branch information
kindermax committed Jul 30, 2022
1 parent 6016851 commit 0d09f74
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ jobs:
go-version: 1.18.x
- name: Checkout code
uses: actions/checkout@v2
- run: go install gotest.tools/gotestsum@latest
- name: Test unit
env:
LETS_CONFIG_DIR: ..
run: go test ./... -v
run: gotestsum --format testname -- ./... -coverprofile=coverage.out

test-bats:
runs-on: ubuntu-latest
Expand Down
8 changes: 4 additions & 4 deletions checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func CalculateChecksumFromSources(workDir string, checksumSources map[string][]s
return checksumMap, nil
}

func ReadChecksumFromDisk(dotLetsDir, cmdName, checksumName string) (string, error) {
_, checksumFilePath := getChecksumPath(dotLetsDir, cmdName, checksumName)
func ReadChecksumFromDisk(checksumsDir, cmdName, checksumName string) (string, error) {
_, checksumFilePath := getChecksumPath(checksumsDir, cmdName, checksumName)

fileData, err := os.ReadFile(checksumFilePath)
if err != nil {
Expand Down Expand Up @@ -203,9 +203,9 @@ func persistOneChecksum(checksumsDir string, cmdName string, checksumName string
}

// IsChecksumForCmdPersisted checks if checksums for cmd exists and persisted.
func IsChecksumForCmdPersisted(dotLetsDir string, cmdName string) bool {
func IsChecksumForCmdPersisted(checksumsDir string, cmdName string) bool {
// check if checksums for cmd exists
if _, err := os.Stat(getCmdChecksumPath(dotLetsDir, cmdName)); err != nil {
if _, err := os.Stat(getCmdChecksumPath(checksumsDir, cmdName)); err != nil {
return !os.IsNotExist(err)
}

Expand Down
4 changes: 2 additions & 2 deletions config/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ func (cmd *Command) GetPersistedChecksums() map[string]string {
}

// ReadChecksumsFromDisk reads all checksums for cmd into map.
func (cmd *Command) ReadChecksumsFromDisk(dotLetsDir string, cmdName string, checksumMap map[string]string) error {
func (cmd *Command) ReadChecksumsFromDisk(checksumsDir string, cmdName string, checksumMap map[string]string) error {
checksums := make(map[string]string, len(checksumMap)+1)

for checksumName := range checksumMap {
filename := checksumName
if checksumName == checksum.DefaultChecksumKey {
filename = checksum.DefaultChecksumFileName
}
checksumResult, err := checksum.ReadChecksumFromDisk(dotLetsDir, cmdName, filename)
checksumResult, err := checksum.ReadChecksumFromDisk(checksumsDir, cmdName, filename)
if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions config/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"

"github.com/lets-cli/lets/config/path"
"github.com/lets-cli/lets/util"
"github.com/lets-cli/lets/workdir"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -71,6 +72,10 @@ func FindConfig(configName string, configDir string) (PathInfo, error) {
return PathInfo{}, fmt.Errorf("can not get .lets absolute path: %w", err)
}

if err := util.SafeCreateDir(dotLetsDir); err != nil {
return PathInfo{}, fmt.Errorf("can not create .lets dir: %w", err)
}

pathInfo := PathInfo{
AbsPath: configAbsPath,
WorkDir: workDir,
Expand Down
36 changes: 33 additions & 3 deletions docs/docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,25 @@ commands:
`key: mixins`

`type: list of string`
`type:`
- `list of strings`
- `list of map`


`Example`

```
mixins:
- lets.build.yaml
- url: https://raw.githubusercontent.com/lets-cli/lets/master/lets.build.yaml
version: 1
```
Allows to split `lets.yaml` into mixins (mixin config files).
To make `lets.yaml` small and readable it is convenient to split main config into many smaller ones and include them
Example:
`Full example`
```yaml
# in lets.yaml
Expand All @@ -159,7 +171,6 @@ commands:
cmd: echo Testing...
```

And `lets test` works fine.

### Ignored mixins

Expand All @@ -178,6 +189,25 @@ mixins:
Now if `my.yaml` exists - it will be loaded as a mixin. If it is not exist - `lets` will skip it.

### Remote mixins `(experimental)`

It is possible to specify mixin as url. Lets will download it and load it as a mixin.
File will be stored in `.lets/mixins` directory.

By default mixin filename will be sha256 hash of url.

You can specify `version` key. If url is not versioned, lets will use `version` for filename hash as well (`hash(url) + hash(version)`).

For example:

```yaml
shell: bash
mixins:
- url: https://raw.githubusercontent.com/lets-cli/lets/master/lets.build.yaml
version: 1
```


### Commands

`key: commands`
Expand Down
6 changes: 0 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/lets-cli/lets/env"
"github.com/lets-cli/lets/logging"
"github.com/lets-cli/lets/runner"
"github.com/lets-cli/lets/workdir"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -31,11 +30,6 @@ func main() {
var rootCmd *cobra.Command
if cfg != nil {
rootCmd = cmd.CreateRootCommandWithConfig(os.Stdout, cfg, version)

if err := workdir.CreateDotLetsDir(cfg.WorkDir); err != nil {
log.Error(err)
os.Exit(1)
}
} else {
rootCmd = cmd.CreateRootCommand(version)
}
Expand Down
6 changes: 3 additions & 3 deletions runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func (r *Runner) initCmd() error {

// if command declared as persist_checksum we must read current persisted checksums into memory
if r.cmd.PersistChecksum {
if checksum.IsChecksumForCmdPersisted(r.cfg.DotLetsDir, r.cmd.Name) {
err := r.cmd.ReadChecksumsFromDisk(r.cfg.DotLetsDir, r.cmd.Name, r.cmd.ChecksumMap)
if checksum.IsChecksumForCmdPersisted(r.cfg.ChecksumsDir, r.cmd.Name) {
err := r.cmd.ReadChecksumsFromDisk(r.cfg.ChecksumsDir, r.cmd.Name, r.cmd.ChecksumMap)
if err != nil {
return fmt.Errorf("failed to read persisted checksum for command '%s': %w", r.cmd.Name, err)
}
Expand Down Expand Up @@ -357,7 +357,7 @@ func (r *Runner) persistChecksum() error {
}

err := checksum.PersistCommandsChecksumToDisk(
r.cfg.DotLetsDir,
r.cfg.ChecksumsDir,
r.cmd.ChecksumMap,
r.cmd.Name,
)
Expand Down
14 changes: 1 addition & 13 deletions workdir/workdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"os"
"path/filepath"

"github.com/lets-cli/lets/util"
log "github.com/sirupsen/logrus"
)

const dotLetsDir = ".lets"

const defaultLetsYaml = `version: %s
const defaultLetsYaml = `version: "%s"
shell: bash
commands:
Expand All @@ -25,17 +24,6 @@ commands:
cmd: echo Hello, "${LETSOPT_NAME:-world}"!
`

// CreateDotLetsDir creates .lets dir where lets.yaml located.
// If directory already exists - skip creation.
func CreateDotLetsDir(workDir string) error {
fullPath, err := GetDotLetsDir(workDir)
if err != nil {
return err
}

return util.SafeCreateDir(fullPath)
}

func GetDotLetsDir(workDir string) (string, error) {
return filepath.Abs(filepath.Join(workDir, dotLetsDir))
}
Expand Down

0 comments on commit 0d09f74

Please sign in to comment.