go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/detector/detector.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package detector 5 6 import ( 7 "runtime" 8 9 "go.mondoo.com/cnquery/providers-sdk/v1/inventory" 10 "go.mondoo.com/cnquery/providers/os/connection" 11 "go.mondoo.com/cnquery/providers/os/connection/shared" 12 ) 13 14 func DetectOS(conn shared.Connection) (*inventory.Platform, bool) { 15 if conn.Type() == connection.Local && runtime.GOOS == "windows" { 16 return WindowsFamily.Resolve(conn) 17 } 18 return OperatingSystems.Resolve(conn) 19 } 20 21 // map that is organized by platform name, to quickly determine its families 22 var osTree = platformParents(OperatingSystems) 23 24 func platformParents(r *PlatformResolver) map[string][]string { 25 return traverseFamily(r, []string{}) 26 } 27 28 func traverseFamily(r *PlatformResolver, parents []string) map[string][]string { 29 if r.IsFamily { 30 // make sure we completely copy the values, otherwise they are going to overwrite themselves 31 p := make([]string, len(parents)) 32 copy(p, parents) 33 // add the current family 34 p = append(p, r.Name) 35 res := map[string][]string{} 36 37 // iterate over children 38 for i := range r.Children { 39 child := r.Children[i] 40 // recursively walk through the tree 41 collect := traverseFamily(child, p) 42 for k := range collect { 43 res[k] = collect[k] 44 } 45 } 46 return res 47 } 48 49 // return child (no family) 50 return map[string][]string{ 51 r.Name: parents, 52 } 53 } 54 55 func Family(platform string) []string { 56 parents, ok := osTree[platform] 57 if !ok { 58 return []string{} 59 } 60 return parents 61 } 62 63 // gathers the family for the provided platform 64 // NOTE: at this point only operating systems have families 65 func IsFamily(platform string, family string) bool { 66 // 1. determine the families of the platform 67 parents, ok := osTree[platform] 68 if !ok { 69 return false 70 } 71 72 // 2. check that the platform is part of the family 73 for i := range parents { 74 if parents[i] == family { 75 return true 76 } 77 } 78 return false 79 }