style: add blank lines between declarations and statement blocks
Separate top-level declarations and multi-line statements with blank lines, keeping an "if err != nil" check next to the statement it belongs to. Expand semicolon one-liners, group the nativeTransport fields by purpose, and format the cgo preamble as readable C. Apart from the expanded one-liners and the C preamble, the change is whitespace only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec0297fd23
commit
a6fda8f8d3
25 files changed
+929
-41
No files matched your search
@@ -49,6 +49,7 @@ func LoadState(path string) (*State, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
var s State
|
||||
d := json.NewDecoder(f)
|
||||
@@ -56,23 +57,29 @@ func LoadState(path string) (*State, error) {
|
||||
if err := d.Decode(&s); err != nil {
|
||||
return nil, fmt.Errorf("state: %w", err)
|
||||
}
|
||||
|
||||
if err := d.Decode(new(any)); !errors.Is(err, io.EOF) {
|
||||
return nil, errors.New("state must contain exactly one JSON object")
|
||||
}
|
||||
|
||||
id, err := hex.DecodeString(s.ID)
|
||||
if s.Version != stateVersion || err != nil || len(id) != 16 {
|
||||
return nil, errors.New("unsupported or invalid state")
|
||||
}
|
||||
|
||||
if s.Phase != "syncing" && s.Phase != "ready-for-cutover" {
|
||||
return nil, errors.New("invalid state phase")
|
||||
}
|
||||
|
||||
names := map[string]bool{}
|
||||
for _, snap := range s.Snapshots {
|
||||
if !validName(snap.Name) || names[snap.Name] || (snap.Done && (snap.CreatedAt == "" || snap.TargetCreatedAt == "")) {
|
||||
return nil, errors.New("invalid snapshot state")
|
||||
}
|
||||
|
||||
names[snap.Name] = true
|
||||
}
|
||||
|
||||
if s.LiveSnapshot != "" {
|
||||
confirmed := false
|
||||
for _, snap := range s.Snapshots {
|
||||
@@ -81,26 +88,32 @@ func LoadState(path string) (*State, error) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !confirmed {
|
||||
return nil, errors.New("live base must reference a completed snapshot")
|
||||
}
|
||||
}
|
||||
|
||||
if s.Phase == "ready-for-cutover" {
|
||||
if len(s.Snapshots) == 0 {
|
||||
return nil, errors.New("ready state has no snapshots")
|
||||
}
|
||||
|
||||
for _, snap := range s.Snapshots {
|
||||
if !snap.Done {
|
||||
return nil, errors.New("ready state has incomplete snapshots")
|
||||
}
|
||||
}
|
||||
|
||||
if !s.Snapshots[len(s.Snapshots)-1].Final {
|
||||
return nil, errors.New("ready state has no final snapshot")
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.Config.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("state config: %w", err)
|
||||
}
|
||||
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
@@ -116,6 +129,7 @@ func atomicJSON(path string, v any) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
name := f.Name()
|
||||
defer os.Remove(name)
|
||||
defer f.Close()
|
||||
@@ -124,19 +138,24 @@ func atomicJSON(path string, v any) error {
|
||||
if err = enc.Encode(v); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = f.Sync(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = f.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = os.Rename(name, path); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d, err := os.Open(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer d.Close()
|
||||
return d.Sync()
|
||||
}
|
||||
@@ -146,9 +165,11 @@ func lockFile(path string) (*os.File, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
|
||||
f.Close()
|
||||
return nil, fmt.Errorf("another process holds lock %s: %w", path, err)
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
Reference in new issue
Block a user