agola/internal/services/configstore/common/common.go

134 lines
3.6 KiB
Go
Raw Normal View History

2019-02-21 15:08:30 +00:00
// Copyright 2019 Sorint.lab
//
// 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 common
import (
"fmt"
"net/url"
2019-02-21 15:08:30 +00:00
"path"
"strings"
"github.com/sorintlab/agola/internal/services/types"
2019-02-21 15:08:30 +00:00
)
var (
// Storage paths. Always use path (not filepath) to use the "/" separator
StorageDataDir = "data"
StorageUsersDir = path.Join(StorageDataDir, "users")
2019-02-28 14:52:35 +00:00
StorageOrgsDir = path.Join(StorageDataDir, "orgs")
StorageProjectsDir = path.Join(StorageDataDir, "projects")
StorageProjectGroupsDir = path.Join(StorageDataDir, "projectgroups")
2019-02-21 15:08:30 +00:00
StorageRemoteSourcesDir = path.Join(StorageDataDir, "remotesources")
2019-03-14 13:36:18 +00:00
StorageSecretsDir = path.Join(StorageDataDir, "secrets")
StorageVariablesDir = path.Join(StorageDataDir, "variables")
2019-02-21 15:08:30 +00:00
)
const (
etcdWalsMinRevisionRange = 100
)
func StorageUserFile(userID string) string {
return path.Join(StorageUsersDir, userID)
}
2019-02-28 14:52:35 +00:00
func StorageOrgFile(orgID string) string {
return path.Join(StorageOrgsDir, orgID)
}
func StorageProjectGroupFile(projectGroupID string) string {
return path.Join(StorageProjectGroupsDir, projectGroupID)
2019-02-21 15:08:30 +00:00
}
func StorageProjectFile(projectID string) string {
return path.Join(StorageProjectsDir, projectID)
}
2019-02-21 15:08:30 +00:00
func StorageRemoteSourceFile(userID string) string {
return path.Join(StorageRemoteSourcesDir, userID)
}
2019-02-21 15:08:30 +00:00
2019-03-14 13:36:18 +00:00
func StorageSecretFile(secretID string) string {
return path.Join(StorageSecretsDir, secretID)
}
func StorageVariableFile(variableID string) string {
return path.Join(StorageVariablesDir, variableID)
}
func PathToTypeID(p string) (types.ConfigType, string) {
var configType types.ConfigType
2019-02-21 15:08:30 +00:00
switch path.Dir(p) {
case StorageUsersDir:
configType = types.ConfigTypeUser
2019-02-28 14:52:35 +00:00
case StorageOrgsDir:
configType = types.ConfigTypeOrg
case StorageProjectGroupsDir:
configType = types.ConfigTypeProjectGroup
case StorageProjectsDir:
configType = types.ConfigTypeProject
2019-02-21 15:08:30 +00:00
case StorageRemoteSourcesDir:
configType = types.ConfigTypeRemoteSource
2019-03-14 13:36:18 +00:00
case StorageSecretsDir:
configType = types.ConfigTypeSecret
case StorageVariablesDir:
configType = types.ConfigTypeVariable
2019-02-21 15:08:30 +00:00
default:
panic(fmt.Errorf("cannot determine configtype for path: %q", p))
}
return configType, path.Base(p)
}
func DataToPathFunc(dataType string, id string) string {
switch types.ConfigType(dataType) {
case types.ConfigTypeUser:
return StorageUserFile(id)
case types.ConfigTypeOrg:
return StorageOrgFile(id)
case types.ConfigTypeProjectGroup:
return StorageProjectGroupFile(id)
case types.ConfigTypeProject:
return StorageProjectFile(id)
case types.ConfigTypeRemoteSource:
return StorageRemoteSourceFile(id)
case types.ConfigTypeSecret:
return StorageSecretFile(id)
case types.ConfigTypeVariable:
return StorageVariableFile(id)
}
panic(fmt.Errorf("unknown data type %q", dataType))
}
type RefType int
const (
RefTypeID RefType = iota
RefTypePath
)
// ParseRef parses the api call to determine if the provided ref is
// an ID or a path
func ParseRef(projectRef string) (RefType, error) {
projectRef, err := url.PathUnescape(projectRef)
if err != nil {
return -1, err
}
if strings.Contains(projectRef, "/") {
return RefTypePath, nil
}
return RefTypeID, nil
}