-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c45ffe1
commit c9a537e
Showing
7 changed files
with
159 additions
and
5 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
File renamed without changes.
File renamed without changes.
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,111 @@ | ||
package alt | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/gofrs/flock" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/platformsh/cli/internal/config" | ||
"github.com/platformsh/cli/internal/state" | ||
) | ||
|
||
func ShouldAutoUpdate(cnf *config.Config) bool { | ||
if !cnf.Updates.Check { | ||
return false | ||
} | ||
src := config.GetSource() | ||
if src.Type != config.SourceTypeFile { | ||
return false | ||
} | ||
cnfPath := src.Value | ||
if cnfPath == "" { | ||
return false | ||
} | ||
if cnf.Metadata.URL == "" { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
const defaultUpdateInterval = 21600 // 6 hours | ||
|
||
func AutoUpdate(ctx context.Context, cnf *config.Config, debugLog func(fmt string, i ...any)) error { | ||
s, err := state.Load(cnf) | ||
interval := cnf.Metadata.UpdateInterval | ||
if interval == 0 { | ||
interval = defaultUpdateInterval | ||
} | ||
if err == nil && int(time.Now().Unix()-s.ConfigUpdates.LastChecked) < interval { | ||
return nil | ||
} | ||
|
||
defer func() { | ||
s.ConfigUpdates.LastChecked = time.Now().Unix() | ||
if err := state.Save(s, cnf); err != nil { | ||
debugLog("Error saving state: %s", err) | ||
} | ||
}() | ||
|
||
src := config.GetSource() | ||
if src.Type != config.SourceTypeFile || src.Value == "" { | ||
return fmt.Errorf("no config file path available") | ||
} | ||
if cnf.Metadata.URL == "" { | ||
return fmt.Errorf("no config URL available") | ||
} | ||
cnfPath := src.Value | ||
|
||
lock := flock.New(cnfPath) | ||
locked, err := lock.TryLock() | ||
if err != nil { | ||
return fmt.Errorf("failed to lock configuration file: %w", err) | ||
} | ||
defer func() { | ||
if err := lock.Unlock(); err != nil { | ||
debugLog("Failed to unlock configuration file: %s", err) | ||
} | ||
}() | ||
if !locked { | ||
debugLog("Not updating configuration: could not lock file: %s", cnfPath) | ||
return nil | ||
} | ||
|
||
f, err := os.OpenFile(cnfPath, os.O_WRONLY, 0600) | ||
if err != nil { | ||
return fmt.Errorf("failed to open config file for writing: %w", err) | ||
} | ||
defer f.Close() | ||
stat, err := f.Stat() | ||
if err != nil { | ||
return fmt.Errorf("could not stat config file %s: %w", cnfPath, err) | ||
} | ||
if time.Since(stat.ModTime()) < time.Duration(interval)*time.Second { | ||
debugLog("Config file updated recently: %s", cnfPath) | ||
return nil | ||
} | ||
|
||
debugLog("Checking for configuration updates from URL: %s", cnf.Metadata.URL) | ||
newCnfNode, newCnfStruct, err := FetchConfig(ctx, cnf.Metadata.URL) | ||
if err != nil { | ||
return err | ||
} | ||
if !cnf.Metadata.UpdatedAt.IsZero() && !newCnfStruct.Metadata.UpdatedAt.IsZero() && | ||
cnf.Metadata.UpdatedAt.After(newCnfStruct.Metadata.UpdatedAt) { | ||
debugLog("Skipping auto update: current config updated after remote config") | ||
return nil | ||
} | ||
b, err := yaml.Marshal(newCnfNode) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err := f.Write(b); err != nil { | ||
return err | ||
} | ||
debugLog("Automatically updated config file: %s", cnfPath) | ||
|
||
return nil | ||
} |
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
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