1// Copyright 2020 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Package codebase let's you manage codebases 16package codebase 17 18import ( 19 "fmt" 20 "os" 21 "path/filepath" 22 23 "android.googlesource.com/platform/tools/treble.git/hacksaw/config" 24) 25 26// Add a codebase to the list of supported codebases 27func Add(name string, path string) (*config.Config, error) { 28 absPath, err := filepath.Abs(path) 29 if err != nil { 30 return nil, err 31 } 32 //check that the codebase path is valid before adding 33 _, err = os.Stat(absPath) 34 if err != nil { 35 return nil, err 36 } 37 cfg := config.GetConfig() 38 if _, ok := cfg.Codebases[name]; ok { 39 return cfg, fmt.Errorf("Codebase %s already exists", name) 40 } 41 cfg.Codebases[name] = absPath 42 return cfg, err 43} 44 45// Remove an existing codebase 46func Remove(remove string) (*config.Config, error) { 47 cfg := config.GetConfig() 48 _, ok := cfg.Codebases[remove] 49 if !ok { 50 return nil, fmt.Errorf("Codebase %s not found", remove) 51 } 52 delete(cfg.Codebases, remove) 53 if cfg.DefaultCodebase == remove { 54 cfg.DefaultCodebase = "" 55 } 56 return cfg, nil 57} 58 59// Default gets the default codebase 60func Default() string { 61 cfg := config.GetConfig() 62 def := cfg.DefaultCodebase 63 return def 64} 65 66// SetDefault sets the default codebase 67func SetDefault(def string) error { 68 cfg := config.GetConfig() 69 _, ok := cfg.Codebases[def] 70 if !ok { 71 return fmt.Errorf("Codebase %s not found", def) 72 } 73 cfg.DefaultCodebase = def 74 return nil 75} 76 77// List all supported codebases 78func List() map[string]string { 79 cfg := config.GetConfig() 80 return cfg.Codebases 81} 82 83// GetDir retrieves the directory of a specific workspace 84func GetDir(codebase string) (string, error) { 85 cfg := config.GetConfig() 86 dir, ok := cfg.Codebases[codebase] 87 if !ok { 88 return dir, fmt.Errorf("Codebase %s not found", 89 codebase) 90 } 91 return dir, nil 92} 93