|
4 | 4 | "fmt" |
5 | 5 | "os" |
6 | 6 | "path" |
| 7 | + "slices" |
7 | 8 |
|
8 | 9 | configUtil "github.com/microcks/microcks-cli/pkg/util" |
9 | 10 | ) |
@@ -60,6 +61,16 @@ type Auth struct { |
60 | 61 | ClientSecret string |
61 | 62 | } |
62 | 63 |
|
| 64 | +type WatchConfig struct { |
| 65 | + Entries []WatchEntry `yaml:"entries"` |
| 66 | +} |
| 67 | + |
| 68 | +type WatchEntry struct { |
| 69 | + FilePath string `yaml:"filePath"` |
| 70 | + Context []string `yaml:"context"` |
| 71 | + MainArtifact bool `yaml:"mainartifact"` |
| 72 | +} |
| 73 | + |
63 | 74 | // ReadLocalConfig loads up the local configuration file. Returns nil if config does not exist |
64 | 75 | func ReadLocalConfig(path string) (*LocalConfig, error) { |
65 | 76 | var err error |
@@ -123,6 +134,14 @@ func DefaultLocalConfigPath() (string, error) { |
123 | 134 | return path.Join(dir, "config"), nil |
124 | 135 | } |
125 | 136 |
|
| 137 | +func DefaultLocalWatchPath() (string, error) { |
| 138 | + dir, err := DefaultConfigDir() |
| 139 | + if err != nil { |
| 140 | + return "", err |
| 141 | + } |
| 142 | + return path.Join(dir, "watch"), nil |
| 143 | +} |
| 144 | + |
126 | 145 | func ValidateLocalConfig(config LocalConfig) error { |
127 | 146 | if config.CurrentContext == "" { |
128 | 147 | return nil |
@@ -331,3 +350,45 @@ func (l *LocalConfig) RemoveAuth(server string) bool { |
331 | 350 | } |
332 | 351 | return false |
333 | 352 | } |
| 353 | + |
| 354 | +func (w *WatchConfig) UpsertEntry(entry WatchEntry) { |
| 355 | + for i, e := range w.Entries { |
| 356 | + if e.FilePath == entry.FilePath { |
| 357 | + contexts := w.Entries[i].Context |
| 358 | + if !slices.Contains(contexts, entry.Context[0]) { |
| 359 | + entry.Context = append(entry.Context, contexts...) |
| 360 | + } |
| 361 | + w.Entries[i] = entry |
| 362 | + return |
| 363 | + } |
| 364 | + } |
| 365 | + w.Entries = append(w.Entries, entry) |
| 366 | +} |
| 367 | + |
| 368 | +func ReadLocalWatchConfig(path string) (*WatchConfig, error) { |
| 369 | + var err error |
| 370 | + var config WatchConfig |
| 371 | + |
| 372 | + // check file permission only when microcks config exists |
| 373 | + if fi, err := os.Stat(path); err == nil { |
| 374 | + err = getFilePermission(fi) |
| 375 | + if err != nil { |
| 376 | + return nil, err |
| 377 | + } |
| 378 | + } |
| 379 | + |
| 380 | + err = configUtil.UnmarshalLocalFile(path, &config) |
| 381 | + if os.IsNotExist(err) { |
| 382 | + return nil, nil |
| 383 | + } |
| 384 | + |
| 385 | + return &config, nil |
| 386 | +} |
| 387 | + |
| 388 | +func WriteLocalWatchConfig(config WatchConfig, cfgPath string) error { |
| 389 | + err := os.MkdirAll(path.Dir(cfgPath), os.ModePerm) |
| 390 | + if err != nil { |
| 391 | + return err |
| 392 | + } |
| 393 | + return configUtil.MarshalLocalYAMLFile(cfgPath, &config) |
| 394 | +} |
0 commit comments