Make install idempotent to jsonnetfile.json

This commit is contained in:
Frederic Branczyk 2018-04-24 17:43:27 +01:00
parent cb59934d1d
commit 5233278a99
No known key found for this signature in database
GPG key ID: 7741A52782A90069
2 changed files with 28 additions and 33 deletions

View file

@ -132,7 +132,8 @@ func RunSubcommand(ctx context.Context, cfg config, subcommand string, args []st
name = repo name = repo
} }
} }
m.Dependencies = append(m.Dependencies, spec.Dependency{
newDep := spec.Dependency{
Name: name, Name: name,
Source: spec.Source{ Source: spec.Source{
GitSource: &spec.GitSource{ GitSource: &spec.GitSource{
@ -141,26 +142,25 @@ func RunSubcommand(ctx context.Context, cfg config, subcommand string, args []st
}, },
}, },
Version: version, Version: version,
}) }
} else { oldDeps := m.Dependencies
m.Dependencies = append(m.Dependencies, spec.Dependency{ newDeps := []spec.Dependency{}
Name: args[0], oldDepReplaced := false
Source: spec.Source{ for _, d := range oldDeps {
GitSource: &spec.GitSource{ if d.Name == newDep.Name {
Remote: args[1], newDeps = append(newDeps, newDep)
Subdir: args[2], oldDepReplaced = true
}, } else {
}, newDeps = append(newDeps, d)
}) }
} }
}
{ if !oldDepReplaced {
b, err := json.MarshalIndent(m, "", " ") newDeps = append(newDeps, newDep)
if err != nil { }
return errors.Wrap(err, "failed to encode jsonnet file")
m.Dependencies = newDeps
} }
fmt.Println(string(b))
} }
srcPath := filepath.Join(cfg.JsonnetHome) srcPath := filepath.Join(cfg.JsonnetHome)
@ -180,7 +180,7 @@ func RunSubcommand(ctx context.Context, cfg config, subcommand string, args []st
if err != nil { if err != nil {
return errors.Wrap(err, "failed to create tmp dir") return errors.Wrap(err, "failed to create tmp dir")
} }
//defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
subdir := "" subdir := ""
var p pkg.Interface var p pkg.Interface
@ -211,13 +211,13 @@ func RunSubcommand(ctx context.Context, cfg config, subcommand string, args []st
return errors.Wrap(err, "failed to create parent path") return errors.Wrap(err, "failed to create parent path")
} }
// The path is encoded in the dir, so no need to do anything if it err = os.RemoveAll(destPath)
// already exists. if err != nil {
if _, err := os.Stat(destPath); os.IsNotExist(err) { return errors.Wrap(err, "failed to clean previous destination path")
err := os.Rename(path.Join(tmpDir, subdir), destPath) }
if err != nil { err = os.Rename(path.Join(tmpDir, subdir), destPath)
return errors.Wrap(err, "failed to move package") if err != nil {
} return errors.Wrap(err, "failed to move package")
} }
} }

View file

@ -19,8 +19,7 @@ type JsonnetFile struct {
} }
type Source struct { type Source struct {
GitSource *GitSource `json:"git"` GitSource *GitSource `json:"git"`
GitHubSource *GitHubSource `json:"github"`
} }
type GitSource struct { type GitSource struct {
@ -28,10 +27,6 @@ type GitSource struct {
Subdir string `json:"subdir"` Subdir string `json:"subdir"`
} }
type GitHubSource struct {
Repo string `json:"repo"`
}
type Dependency struct { type Dependency struct {
Name string `json:"name"` Name string `json:"name"`
Source Source `json:"source"` Source Source `json:"source"`