Best Testkube code snippet using config.getDir
workspace.go
Source:workspace.go
1// Copyright 2020 Google LLC2//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 at6//7// https://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.14// Package workspace let's you manage workspaces15package workspace16import (17 "fmt"18 "io/ioutil"19 "os"20 "os/exec"21 "path/filepath"22 "strings"23 "android.googlesource.com/platform/tools/treble.git/hacksaw/bind"24 "android.googlesource.com/platform/tools/treble.git/hacksaw/codebase"25 "android.googlesource.com/platform/tools/treble.git/hacksaw/config"26 "android.googlesource.com/platform/tools/treble.git/hacksaw/git"27)28type Workspace struct {29 composer Composer30 topDir string31}32func New(bm bind.PathBinder, topDir string) Workspace {33 return Workspace{NewComposer(bm), topDir}34}35// Create workspace36func (w Workspace) Create(workspaceName string, codebaseName string) (string, error) {37 cfg := config.GetConfig()38 _, ok := cfg.Codebases[codebaseName]39 if !ok {40 return "", fmt.Errorf("Codebase %s does not exist", codebaseName)41 }42 if _, ok := cfg.Workspaces[workspaceName]; ok {43 return "", fmt.Errorf("Workspace %s already exists", workspaceName)44 }45 cfg.Workspaces[workspaceName] = codebaseName46 workspaceDir, err := w.GetDir(workspaceName)47 if err != nil {48 return "", err49 }50 if err = os.MkdirAll(workspaceDir, os.ModePerm); err != nil {51 return "", err52 }53 codebaseDir, err := codebase.GetDir(codebaseName)54 if err != nil {55 return "", err56 }57 //TODO: match the order of parameters with Create58 if _, err = w.composer.Compose(codebaseDir, workspaceDir); err != nil {59 return "", err60 }61 return workspaceDir, nil62}63// Recreate workspace64func (w Workspace) Recreate(workspaceName string) (string, error) {65 cfg := config.GetConfig()66 codebaseName, ok := cfg.Workspaces[workspaceName]67 if !ok {68 return "", fmt.Errorf("Workspace %s does not exist", workspaceName)69 }70 workspaceDir, err := w.GetDir(workspaceName)71 if err != nil {72 return "", err73 }74 codebaseDir, err := codebase.GetDir(codebaseName)75 if err != nil {76 return "", err77 }78 if _, err = w.composer.Compose(codebaseDir, workspaceDir); err != nil {79 return "", err80 }81 return workspaceDir, nil82}83// GetDir retrieves the directory of a specific workspace84func (w Workspace) GetDir(workspaceName string) (string, error) {85 cfg := config.GetConfig()86 _, ok := cfg.Workspaces[workspaceName]87 if !ok {88 return "", fmt.Errorf("Workspace %s not found", workspaceName)89 }90 dir := filepath.Join(w.topDir, workspaceName)91 return dir, nil92}93// GetCodebase retrieves the codebase that a workspace belongs to94func (w Workspace) GetCodebase(workspaceName string) (string, error) {95 cfg := config.GetConfig()96 codebase, ok := cfg.Workspaces[workspaceName]97 if !ok {98 return "", fmt.Errorf("Workspace %s not found", workspaceName)99 }100 return codebase, nil101}102//SetTopDir sets the directory that contains all workspaces103func (w *Workspace) SetTopDir(dir string) {104 w.topDir = dir105}106func (w Workspace) List() map[string]string {107 cfg := config.GetConfig()108 list := make(map[string]string)109 for name, codebaseName := range cfg.Workspaces {110 list[name] = codebaseName111 }112 return list113}114func (w Workspace) DetachGitWorktrees(workspaceName string, unbindList []string) error {115 workspaceDir, err := w.GetDir(workspaceName)116 if err != nil {117 return err118 }119 workspaceDir, err = filepath.Abs(workspaceDir)120 if err != nil {121 return err122 }123 //resolve all symlinks so it can be124 //matched to mount paths125 workspaceDir, err = filepath.EvalSymlinks(workspaceDir)126 if err != nil {127 return err128 }129 codebaseName, err := w.GetCodebase(workspaceName)130 if err != nil {131 return err132 }133 codebaseDir, err := codebase.GetDir(codebaseName)134 if err != nil {135 return err136 }137 lister := git.NewRepoLister()138 gitProjects, err := lister.List(codebaseDir)139 if err != nil {140 return err141 }142 gitWorktrees := make(map[string]bool)143 for _, project := range gitProjects {144 gitWorktrees[project] = true145 }146 //projects that were unbound were definitely147 //never git worktrees148 for _, unbindPath := range unbindList {149 project, err := filepath.Rel(workspaceDir, unbindPath)150 if err != nil {151 return err152 }153 if _, ok := gitWorktrees[project]; ok {154 gitWorktrees[project] = false155 }156 }157 for project, isWorktree := range gitWorktrees {158 if !isWorktree {159 continue160 }161 codebaseProject := filepath.Join(codebaseDir, project)162 workspaceProject := filepath.Join(workspaceDir, project)163 _, err = os.Stat(workspaceProject)164 if err == nil {165 //proceed to detach166 } else if os.IsNotExist(err) {167 //just skip if it doesn't exist168 continue169 } else {170 return err171 }172 contents, err := ioutil.ReadDir(workspaceProject)173 if err != nil {174 return err175 }176 if len(contents) == 0 {177 //empty directory, not even a .git178 //not a wortree179 continue180 }181 fmt.Print(".")182 cmd := exec.Command("git",183 "-C", codebaseProject,184 "worktree", "remove", "--force", workspaceProject)185 output, err := cmd.CombinedOutput()186 if err != nil {187 return fmt.Errorf("Command\n%s\nfailed with the following:\n%s\n%s",188 cmd.String(), err.Error(), output)189 }190 cmd = exec.Command("git",191 "-C", codebaseProject,192 "branch", "--delete", "--force", workspaceName)193 output, err = cmd.CombinedOutput()194 if err != nil {195 return fmt.Errorf("Command\n%s\nfailed with the following:\n%s\n%s",196 cmd.String(), err.Error(), output)197 }198 }199 return nil200}201func (w Workspace) Remove(remove string) (*config.Config, error) {202 cfg := config.GetConfig()203 _, ok := cfg.Workspaces[remove]204 if !ok {205 return cfg, fmt.Errorf("Workspace %s not found", remove)206 }207 workspaceDir, err := w.GetDir(remove)208 if err != nil {209 return cfg, err210 }211 unbindList, err := w.composer.Dismantle(workspaceDir)212 if err != nil {213 return cfg, err214 }215 fmt.Print("Detaching worktrees")216 if err = w.DetachGitWorktrees(remove, unbindList); err != nil {217 return cfg, err218 }219 fmt.Print("\n")220 fmt.Println("Removing files")221 if err = os.RemoveAll(workspaceDir); err != nil {222 return cfg, err223 }224 delete(cfg.Workspaces, remove)225 return cfg, err226}227func (w Workspace) Edit(editPath string) (string, string, error) {228 editPath, err := filepath.Abs(editPath)229 if err != nil {230 return "", "", err231 }232 editPath, err = filepath.EvalSymlinks(editPath)233 if err != nil {234 return "", "", err235 }236 relProjectPath, err := w.getReadOnlyProjectFromPath(editPath)237 if err != nil {238 return "", "", err239 }240 workspaceName, err := w.getWorkspaceFromPath(editPath)241 if err != nil {242 return "", "", err243 }244 workspaceDir, err := w.GetDir(workspaceName)245 if err != nil {246 return "", "", err247 }248 codebaseName, err := w.GetCodebase(workspaceName)249 if err != nil {250 return "", "", err251 }252 codebaseDir, err := codebase.GetDir(codebaseName)253 if err != nil {254 return "", "", err255 }256 wsProjectPath := filepath.Join(workspaceDir, relProjectPath)257 if err = w.composer.Unbind(wsProjectPath); err != nil {258 return "", "", err259 }260 //TODO: support editing nested projects261 //the command above unbinds nested child projects but262 //we don't rebind them after checking out an editable project branch263 cbProjectPath := filepath.Join(codebaseDir, relProjectPath)264 branchName := workspaceName265 cmd := exec.Command("git",266 "-C", cbProjectPath,267 "worktree", "add",268 "-b", branchName,269 wsProjectPath)270 output, err := cmd.CombinedOutput()271 if err != nil {272 return "", "", fmt.Errorf("Command\n%s\nfailed with the following:\n%s\n%s",273 cmd.String(), err.Error(), output)274 }275 return branchName, wsProjectPath, err276}277func (w Workspace) getReadOnlyProjectFromPath(inPath string) (string, error) {278 worspaceName, err := w.getWorkspaceFromPath(inPath)279 if err != nil {280 return "", err281 }282 workspacePath, err := w.GetDir(worspaceName)283 if err != nil {284 return "", err285 }286 bindList, err := w.composer.List(workspacePath)287 if err != nil {288 return "", err289 }290 for _, bindPath := range bindList {291 if !strings.HasPrefix(inPath+"/", bindPath+"/") {292 continue293 }294 relProjectPath, err := filepath.Rel(workspacePath, bindPath)295 if err != nil {296 return "", err297 }298 return relProjectPath, nil299 }300 return "", fmt.Errorf("Path %s is already editable", inPath)301}302func (w Workspace) getWorkspaceFromPath(inPath string) (string, error) {303 for workspaceName, _ := range w.List() {304 dir, err := w.GetDir(workspaceName)305 if err != nil {306 return "", err307 }308 if strings.HasPrefix(inPath+"/", dir+"/") {309 return workspaceName, nil310 }311 }312 return "", fmt.Errorf("Path %s is not contained in a workspace", inPath)313}...
last_modified_dir_config.go
Source:last_modified_dir_config.go
1package dirconfig2import (3 "errors"4 "fmt"5 "os/exec"6 log "github.com/sirupsen/logrus"7 "github.com/twitter/scoot/common/stats"8)9// Configuration for cleaning disk space of a directory10// based on usage thresholds given as low and high watermarks in KB,11// and retention settings for each threshold as time since last modified in minutes.12// if LowMarkKB <= usage < HighMarkKB, prune files last modified prior to LowRetentionMin13// if HighMarkKB <= usage, prune files last modified prior to HighRetentionMin14type lastModifiedDirConfig struct {15 Dir string16 LowMarkKB uint6417 LowRetentionMin uint18 HighMarkKB uint6419 HighRetentionMin uint20}21func NewLastModifiedDirConfig(dir string, lowMarkKB uint64, lowRetentionMin uint, highMarkKB uint64, highRetentionMin uint) (*lastModifiedDirConfig, error) {22 if lowMarkKB >= highMarkKB {23 return nil, errors.New(24 fmt.Sprintf("Invalid DirConfig for %s: LowMarkKB %d >= HighMarkKB %d", dir, lowMarkKB, highMarkKB))25 }26 return &lastModifiedDirConfig{27 Dir: dir,28 LowMarkKB: lowMarkKB,29 LowRetentionMin: lowRetentionMin,30 HighMarkKB: highMarkKB,31 HighRetentionMin: highRetentionMin,32 }, nil33}34func (dc lastModifiedDirConfig) GetDir() string { return dc.Dir }35func (dc lastModifiedDirConfig) CleanDir() error {36 var usage uint64 = 037 var err error = nil38 if dc.LowMarkKB != 0 || dc.HighMarkKB != 0 {39 usage, err = stats.GetDiskUsageKB(dc.GetDir())40 if err != nil {41 return errors.New(fmt.Sprintf("Failed to Cleanup dir: %s. %s", dc.GetDir(), err))42 }43 }44 if usage >= dc.LowMarkKB && usage < dc.HighMarkKB {45 err = dc.cleanDir(dc.LowRetentionMin)46 if err != nil {47 return errors.New(fmt.Sprintf("Failed to Cleanup dir: %s. %s", dc.GetDir(), err))48 }49 } else if usage >= dc.HighMarkKB {50 err = dc.cleanDir(dc.HighRetentionMin)51 if err != nil {52 return errors.New(fmt.Sprintf("Failed to Cleanup dir: %s. %s", dc.GetDir(), err))53 }54 }55 log.Infof("Not cleaning %s, usage %d(KB) under threshold %d(KB)\n", dc.GetDir(), usage, dc.LowMarkKB)56 return nil57}58func (dc lastModifiedDirConfig) cleanDir(retentionMin uint) error {59 name := "find"60 args := []string{dc.GetDir(), "!", "-path", dc.GetDir(), "-mmin", fmt.Sprintf("+%d", retentionMin), "-delete"}61 log.Infof("Running cleanup for %s with cmd: %s %s\n", dc.GetDir(), name, args)62 _, err := exec.Command(name, args...).Output()63 if err != nil {64 log.Errorf("Error running cleanup command (this can commonly fail due to non-empty directories): %s\n", err)65 if errExit, ok := err.(*exec.ExitError); ok {66 log.Errorf("Cleanup command stderr:\n%s\n", errExit.Stderr)67 }68 return err69 }70 return nil71}...
config.go
Source:config.go
1package main2import (3 "gopkg.in/yaml.v2"4 "io/ioutil"5 "log"6 "os"7 "path"8 "path/filepath"9)10//Global configuration containing SMTP auth, prefs, etc.11var Config *ConfigFile12//ConfigFile is YAML structure of config.yaml file.13type ConfigFile struct {14 Coin, Fiat, Interval string15 Difference float6416 Email struct {17 Username, Password, Server string18 Port int19 Recipient string20 }21}22//LoadConfig loads the config from the config.yaml23//file to the global variable Config as type *ConfigFile.24func LoadConfig() {25 file := path.Join(GetDir(), "config.yaml")26 if _, err := os.Stat(file); os.IsNotExist(err) {27 file = path.Join(GetDir(), "..", "etc", "config.yaml")28 }29 configFile, _ := ioutil.ReadFile(file)30 yaml.Unmarshal(configFile, &Config)31 if Config != nil {32 log.Printf("loaded config: %v", Config)33 } else {34 log.Printf("Error loading config.yaml file in %v.", GetDir())35 os.Exit(1)36 }37}38//GetDir return the current directory holding the executable39func GetDir() string {40 dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))41 return dir42}...
getDir
Using AI Code Generation
1import (2func main() {3 fmt.Println(config.GetDir())4}5import (6func main() {7 fmt.Println(config.GetDir())8}9import (10func main() {11 fmt.Println(config.GetDir())12}13import (14func main() {15 fmt.Println(config.GetDir())16}17import (18func main() {19 fmt.Println(config.GetDir())20}21import (22func main() {23 fmt.Println(config.GetDir())24}25import (26func main() {27 fmt.Println(config.GetDir())28}29import (30func main() {31 fmt.Println(config.GetDir())32}33import (34func main() {35 fmt.Println(config.GetDir())36}37import (38func main() {39 fmt.Println(config.GetDir())40}41import (42func main() {43 fmt.Println(config.GetDir())44}45import (
getDir
Using AI Code Generation
1import (2func main() {3 fmt.Println(config.getDir())4}5import (6func main() {7 fmt.Println(config.getDir())8}9import (10func main() {11 fmt.Println(config.getDir())12}13import (14func main() {15 fmt.Println(config.getDir())16}17import (18func main() {19 fmt.Println(config.getDir())20}21import (22func main() {23 fmt.Println(config.getDir())24}25import (26func main() {27 fmt.Println(config.getDir())28}29import (30func main() {31 fmt.Println(config.getDir())32}33import (34func main() {35 fmt.Println(config.getDir())36}37import (38func main() {39 fmt.Println(config.getDir())40}41import (42func main() {43 fmt.Println(config.getDir())44}45import (46func main() {47 fmt.Println(config.getDir())48}49import (50func main() {51 fmt.Println(config.getDir())52}53import (54func main() {55 fmt.Println(config.getDir
getDir
Using AI Code Generation
1import (2func main() {3 fmt.Println(config.GetDir())4}5import (6func GetDir() string {7 wd, err := os.Getwd()8 if err != nil {9 fmt.Println(err)10 }11}
getDir
Using AI Code Generation
1import "config"2func main() {3 config.getDir()4}5import "config"6func main() {7 config.getDir()8}9import "config"10func main() {11 config.getDir()12}13import "config"14func main() {15 config.getDir()16}17import "config"18func main() {19 config.getDir()20}21import "config"22func main() {23 config.getDir()24}25import "config"26func main() {27 config.getDir()28}29import "config"30func main() {31 config.getDir()32}33import "config"34func main() {35 config.getDir()36}37import "config"38func main() {39 config.getDir()40}41import "config"42func main() {43 config.getDir()44}45import "config"46func main() {47 config.getDir()48}49import "config"50func main() {51 config.getDir()52}53import "config"54func main() {55 config.getDir()56}57import "config"58func main() {59 config.getDir()60}61import "config"62func main() {63 config.getDir()64}
getDir
Using AI Code Generation
1import (2func main() {3 config := goconfig.ConfigFile{4 }5 config.Read()6 fmt.Println("Current directory: ", config.GetDir())7}
getDir
Using AI Code Generation
1import (2type config struct {3}4func (c config) getDir() string {5}6func (c config) setDir(dir string) config {7}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!