1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-24 08:02:18 +02:00
woodpecker/cmd/agent/config.go
6543 3a85559763
Remove code to read agent-id.conf (#2009)
I want to have c805c87e90 published at
least for 2 days ...
... so the migration did happen
2023-07-19 22:09:06 +02:00

74 lines
1.9 KiB
Go

// Copyright 2022 Woodpecker Authors
// Copyright 2019 Laszlo Fogas
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"encoding/json"
"os"
"strings"
"github.com/rs/zerolog/log"
)
type AgentConfig struct {
AgentID int64 `json:"agent_id"`
}
const defaultAgentIDValue = int64(-1)
func readAgentConfig(agentConfigPath string) AgentConfig {
conf := AgentConfig{
AgentID: defaultAgentIDValue,
}
rawAgentConf, err := os.ReadFile(agentConfigPath)
if err != nil {
if os.IsNotExist(err) {
log.Info().Msgf("no agent config found at '%s', start with defaults", agentConfigPath)
} else {
log.Error().Err(err).Msgf("could not open agent config at '%s'", agentConfigPath)
}
return conf
}
if strings.TrimSpace(string(rawAgentConf)) == "" {
return conf
}
if err := json.Unmarshal(rawAgentConf, &conf); err != nil {
log.Error().Err(err).Msg("could not parse agent config")
}
return conf
}
func writeAgentConfig(conf AgentConfig, agentConfigPath string) {
rawAgentConf, err := json.Marshal(conf)
if err != nil {
log.Error().Err(err).Msg("could not marshal agent config")
return
}
// get old config
oldRawAgentConf, _ := os.ReadFile(agentConfigPath)
// if config differ write to disk
if !bytes.Equal(rawAgentConf, oldRawAgentConf) {
if err := os.WriteFile(agentConfigPath, rawAgentConf, 0o644); err != nil {
log.Error().Err(err).Msgf("could not persist agent config at '%s'", agentConfigPath)
}
}
}