github.com/helmwave/helmwave@v0.36.4-0.20240509190856-b35563eba4c6/pkg/plan/import.go (about) 1 package plan 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "io/fs" 8 "os" 9 "path/filepath" 10 "strings" 11 12 "github.com/helmwave/helmwave/pkg/release/uniqname" 13 "github.com/helmwave/helmwave/pkg/version" 14 log "github.com/sirupsen/logrus" 15 ) 16 17 // Import parses directory with plan files and imports them into structure. 18 func (p *Plan) Import(ctx context.Context) error { 19 body, err := NewBody(ctx, p.fullPath, true) 20 if err != nil { 21 return err 22 } 23 24 err = p.importManifest() 25 26 switch { 27 case errors.Is(err, ErrManifestDirEmpty), errors.Is(err, fs.ErrNotExist): 28 log.WithError(err).Warn("error caught while importing manifests") 29 case err != nil: 30 return err 31 } 32 33 p.body = body 34 35 // Validate all files exist. 36 err = p.ValidateValuesImport() 37 if err != nil { 38 return err 39 } 40 41 version.Validate(p.body.Version) 42 43 return nil 44 } 45 46 func (p *Plan) importManifest() error { 47 d := filepath.Join(p.dir, Manifest) 48 ls, err := os.ReadDir(d) 49 if err != nil { 50 return fmt.Errorf("failed to read manifest dir %s: %w", d, err) 51 } 52 53 if len(ls) == 0 { 54 return ErrManifestDirEmpty 55 } 56 57 for _, l := range ls { 58 if l.IsDir() { 59 continue 60 } 61 62 f := filepath.Join(p.dir, Manifest, l.Name()) 63 c, err := os.ReadFile(f) 64 if err != nil { 65 return fmt.Errorf("failed to read manifest %s: %w", f, err) 66 } 67 68 n := strings.TrimSuffix(l.Name(), filepath.Ext(l.Name())) // drop extension of file 69 70 p.manifests[uniqname.UniqName(n)] = string(c) 71 } 72 73 return nil 74 }