go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/_motor/discovery/common/projectname.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package common 5 6 import ( 7 "os" 8 "path" 9 "path/filepath" 10 "strings" 11 ) 12 13 func ProjectNameFromPath(file string) string { 14 // if it is a local file (which may not be true) 15 name := "" 16 fi, err := os.Stat(file) 17 if err == nil { 18 if fi.IsDir() && fi.Name() != "." { 19 name = "directory " + fi.Name() 20 } else if fi.IsDir() { 21 name = fi.Name() 22 } else { 23 name = filepath.Base(fi.Name()) 24 extension := filepath.Ext(name) 25 name = strings.TrimSuffix(name, extension) 26 } 27 } else { 28 // it is not a local file, so we try to be a bit smart 29 name = path.Base(file) 30 extension := path.Ext(name) 31 name = strings.TrimSuffix(name, extension) 32 } 33 34 // if the path is . we read the current directory 35 if name == "." { 36 abspath, err := filepath.Abs(name) 37 if err == nil { 38 name = ProjectNameFromPath(abspath) 39 } 40 } 41 42 return name 43 }