75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package common
|
|
|
|
/*
|
|
* This file is part of Autogits.
|
|
*
|
|
* Copyright © 2024 SUSE LLC
|
|
*
|
|
* Autogits is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
* Foundation, either version 2 of the License, or (at your option) any later
|
|
* version.
|
|
*
|
|
* Autogits is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* Foobar. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
type AutogitConfig struct {
|
|
Workflows []string // [pr, direct, test]
|
|
Organization string
|
|
GitProjectName string // Organization/GitProjectName.git is PrjGit
|
|
Branch string // branch name of PkgGit that aligns with PrjGit submodules
|
|
}
|
|
|
|
func ReadWorkflowConfigs(reader io.Reader) ([]*AutogitConfig, error) {
|
|
data, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error reading config file. err: %w", err)
|
|
}
|
|
|
|
var config []*AutogitConfig
|
|
if err = json.Unmarshal(data, &config); err != nil {
|
|
return nil, fmt.Errorf("Error parsing config file. err: %w", err)
|
|
}
|
|
|
|
availableWorkflows := []string{"pr", "direct", "test"}
|
|
for _, workflow := range config {
|
|
for _, w := range workflow.Workflows {
|
|
if !slices.Contains(availableWorkflows, w) {
|
|
return nil, fmt.Errorf(
|
|
"Invalid Workflow '%s'. Only available workflows are: %s",
|
|
w, strings.Join(availableWorkflows, " "),
|
|
)
|
|
}
|
|
}
|
|
if len(workflow.GitProjectName) == 0 {
|
|
workflow.GitProjectName = DefaultGitPrj
|
|
}
|
|
}
|
|
|
|
return config, nil
|
|
}
|
|
|
|
func ReadWorkflowConfigsFile(filename string) ([]*AutogitConfig, error) {
|
|
file, err := os.Open(filename)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Cannot open config file for reading. err: %w", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
return ReadWorkflowConfigs(file)
|
|
}
|