github.com/nuvolaris/nuv@v0.0.0-20240511174247-a74e3a52bfd8/common.go (about) 1 // Licensed to the Apache Software Foundation (ASF) under one 2 // or more contributor license agreements. See the NOTICE file 3 // distributed with this work for additional information 4 // regarding copyright ownership. The ASF licenses this file 5 // to you under the Apache License, Version 2.0 (the 6 // "License"); you may not use this file except in compliance 7 // with the License. You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package main 19 20 import ( 21 "encoding/json" 22 "fmt" 23 "log" 24 "os" 25 "path/filepath" 26 "strings" 27 ) 28 29 // default files 30 const NUVFILE = "nuvfile.yml" 31 const NUVROOT = "nuvroot.json" 32 const NUVOPTS = "nuvopts.txt" 33 const CONFIGFILE = "config.json" 34 35 // repo where download tasks 36 const NUVREPO = "http://github.com/nuvolaris/olaris" 37 38 // branch where download tasks 39 // defaults to test - will be changed in compilation 40 var NuvVersion = "main" 41 var NuvBranch = "3.0.0" 42 43 // Represents nuvroot.json. It is used to parse the file. 44 type NuvRootJSON struct { 45 Version string `json:"version"` 46 Config map[string]interface{} `json:"config"` 47 } 48 49 // default port for nuv server 50 const DefaultNuvPort = 9768 51 52 func getNuvPort() string { 53 port := os.Getenv("NUV_PORT") 54 if port == "" { 55 port = fmt.Sprintf("%d", DefaultNuvPort) 56 } 57 //nolint:errcheck 58 os.Setenv("NUV_PORT", port) 59 return port 60 } 61 62 // get defaults 63 func getNuvRoot() (string, error) { 64 root := os.Getenv("NUV_ROOT") 65 if root == "" { 66 dir, err := os.Getwd() 67 if err == nil { 68 root, err = locateNuvRoot(dir) 69 } 70 if err != nil { 71 return "", err 72 } 73 } 74 //nolint:errcheck 75 os.Setenv("NUV_ROOT", root) 76 return root, nil 77 } 78 79 func getNuvRepo() string { 80 repo := os.Getenv("NUV_REPO") 81 if repo == "" { 82 repo = NUVREPO 83 } 84 //nolint:errcheck 85 os.Setenv("NUV_REPO", repo) 86 return repo 87 } 88 89 func getNuvBranch() string { 90 branch := os.Getenv("NUV_BRANCH") 91 if branch == "" { 92 branch = NuvBranch 93 } 94 //nolint:errcheck 95 os.Setenv("NUV_BRANCH", branch) 96 return branch 97 } 98 99 func readNuvRootFile(dir string) (NuvRootJSON, error) { 100 data := NuvRootJSON{} 101 json_buf, err := os.ReadFile(joinpath(dir, NUVROOT)) 102 if err != nil { 103 return NuvRootJSON{}, err 104 } 105 if err := json.Unmarshal(json_buf, &data); err != nil { 106 warn("nuvroot.json parsed with an error", err) 107 } 108 return data, nil 109 } 110 111 // utils 112 func joinpath(dir string, file string) string { 113 return filepath.Join(dir, file) 114 } 115 116 func split(s string) []string { 117 return strings.Fields(s) 118 } 119 120 func exists(dir string, file string) bool { 121 _, err := os.Stat(joinpath(dir, file)) 122 return !os.IsNotExist(err) 123 } 124 125 func isDir(fileOrDir string) bool { 126 fileInfo, err := os.Stat(fileOrDir) 127 if err != nil { 128 debug(err) 129 return false 130 } 131 132 // Check if the file is a directory 133 if fileInfo.IsDir() { 134 return true 135 } 136 return false 137 } 138 139 func parent(dir string) string { 140 return filepath.Dir(dir) 141 } 142 143 func readfile(file string) string { 144 dat, err := os.ReadFile(file) 145 if err != nil { 146 return "" 147 } 148 return string(dat) 149 } 150 151 //var logger log.Logger = log.New(os.Stderr, "", 0) 152 153 func warn(args ...any) { 154 log.Println(args...) 155 } 156 157 var tracing = os.Getenv("TRACE") != "" 158 159 func trace(args ...any) { 160 if tracing { 161 log.Println(append([]any{"TRACE: "}, args...)) 162 } 163 } 164 165 var debugging = os.Getenv("DEBUG") != "" 166 167 func debug(args ...any) { 168 if debugging { 169 log.Println(append([]any{"DEBUG: "}, args...)) 170 } 171 } 172 func debugf(format string, args ...any) { 173 if debugging { 174 log.Printf("DEBUG: "+format+"\n", args...) 175 } 176 }