-
-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to store logs in files (#3568)
Co-authored-by: Anbraten <6918444+anbraten@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
135 additions
and
4 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
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
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
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,82 @@ | ||
package file | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"go.woodpecker-ci.org/woodpecker/v2/server/model" | ||
"go.woodpecker-ci.org/woodpecker/v2/server/services/log" | ||
) | ||
|
||
type logStore struct { | ||
base string | ||
} | ||
|
||
func NewLogStore(base string) (log.Service, error) { | ||
if base == "" { | ||
return nil, fmt.Errorf("file storage base path is required") | ||
} | ||
if _, err := os.Stat(base); err != nil && os.IsNotExist(err) { | ||
err = os.MkdirAll(base, 0o600) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return logStore{base: base}, nil | ||
} | ||
|
||
func (l logStore) filePath(id int64) string { | ||
return filepath.Join(l.base, fmt.Sprintf("%d.json", id)) | ||
} | ||
|
||
func (l logStore) LogFind(step *model.Step) ([]*model.LogEntry, error) { | ||
filename := l.filePath(step.ID) | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
s := bufio.NewScanner(file) | ||
var entries []*model.LogEntry | ||
for s.Scan() { | ||
j := s.Text() | ||
if len(strings.TrimSpace(j)) == 0 { | ||
continue | ||
} | ||
entry := &model.LogEntry{} | ||
err = json.Unmarshal([]byte(j), entry) | ||
if err != nil { | ||
return nil, err | ||
} | ||
entries = append(entries, entry) | ||
} | ||
|
||
return entries, nil | ||
} | ||
|
||
func (l logStore) LogAppend(logEntry *model.LogEntry) error { | ||
file, err := os.OpenFile(l.filePath(logEntry.StepID), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) | ||
if err != nil { | ||
return err | ||
} | ||
jsonData, err := json.Marshal(logEntry) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = file.Write(append(jsonData, byte('\n'))) | ||
if err != nil { | ||
return err | ||
} | ||
return file.Close() | ||
} | ||
|
||
func (l logStore) LogDelete(step *model.Step) error { | ||
return os.Remove(l.filePath(step.ID)) | ||
} |
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,9 @@ | ||
package log | ||
|
||
import "go.woodpecker-ci.org/woodpecker/v2/server/model" | ||
|
||
type Service interface { | ||
LogFind(step *model.Step) ([]*model.LogEntry, error) | ||
LogAppend(logEntry *model.LogEntry) error | ||
LogDelete(step *model.Step) error | ||
} |