forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate IDs using file path of runbooks.
- Loading branch information
Showing
3 changed files
with
155 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package runn | ||
|
||
import ( | ||
"crypto/sha1" //#nosec G505 | ||
"encoding/hex" | ||
"errors" | ||
"io" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/rs/xid" | ||
"github.com/samber/lo" | ||
) | ||
|
||
// generateIDsUsingPath generates IDs using path of runbooks. | ||
// ref: https://github.com/k1LoW/runn/blob/main/docs/designs/id.md | ||
func generateIDsUsingPath(ops []*operator) error { | ||
if len(ops) == 0 { | ||
return nil | ||
} | ||
type tmp struct { | ||
o *operator | ||
p string | ||
rp []string | ||
id string | ||
} | ||
var ss []*tmp | ||
max := 0 | ||
for _, o := range ops { | ||
p, err := filepath.Abs(filepath.Clean(o.bookPath)) | ||
if err != nil { | ||
return err | ||
} | ||
rp := reversePath(p) | ||
ss = append(ss, &tmp{ | ||
o: o, | ||
p: p, | ||
rp: rp, | ||
}) | ||
if len(rp) >= max { | ||
max = len(rp) | ||
} | ||
} | ||
for i := 1; i <= max; i++ { | ||
ids := []string{} | ||
for _, s := range ss { | ||
var ( | ||
id string | ||
err error | ||
) | ||
if len(s.rp) < i { | ||
id, err = generateID(strings.Join(s.rp, "/")) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
id, err = generateID(strings.Join(s.rp[:i], "/")) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
s.id = id | ||
ids = append(ids, id) | ||
} | ||
if len(lo.Uniq(ids)) == len(ss) { | ||
// Set ids | ||
for _, s := range ss { | ||
s.o.id = s.id | ||
} | ||
return nil | ||
} | ||
} | ||
return errors.New("failed to generate ids") | ||
} | ||
|
||
func generateID(p string) (string, error) { | ||
if p == "" { | ||
return generateRandomID() | ||
} | ||
h := sha1.New() //#nosec G401 | ||
if _, err := io.WriteString(h, p); err != nil { | ||
return "", err | ||
} | ||
return hex.EncodeToString(h.Sum(nil)), nil | ||
} | ||
|
||
func generateRandomID() (string, error) { | ||
h := sha1.New() //#nosec G401 | ||
if _, err := io.WriteString(h, xid.New().String()); err != nil { | ||
return "", err | ||
} | ||
return hex.EncodeToString(h.Sum(nil)), nil | ||
} | ||
|
||
func reversePath(p string) []string { | ||
return lo.Reverse(strings.Split(filepath.ToSlash(p), "/")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters