go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/python_package.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package resources
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  
    10  	"github.com/spf13/afero"
    11  	"go.mondoo.com/cnquery/llx"
    12  	"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
    13  	"go.mondoo.com/cnquery/providers/os/connection/shared"
    14  )
    15  
    16  func (k *mqlPythonPackage) id() (string, error) {
    17  	file := k.GetFile()
    18  	if file.Error != nil {
    19  		return "", file.Error
    20  	}
    21  
    22  	mqlFile := file.Data
    23  	metadataPath := mqlFile.Path.Data
    24  	return metadataPath, nil
    25  }
    26  
    27  func initPythonPackage(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
    28  	if len(args) > 1 {
    29  		return args, nil, nil
    30  	}
    31  	if x, ok := args["path"]; ok {
    32  		path, ok := x.Value.(string)
    33  		if !ok {
    34  			return nil, nil, errors.New("Wrong type for 'path' in python.package initialization, it must be a string")
    35  		}
    36  
    37  		file, err := CreateResource(runtime, "file", map[string]*llx.RawData{
    38  			"path": llx.StringData(path),
    39  		})
    40  		if err != nil {
    41  			return nil, nil, err
    42  		}
    43  		args["file"] = llx.ResourceData(file, "file")
    44  
    45  		delete(args, "path")
    46  	}
    47  	return args, nil, nil
    48  }
    49  
    50  func (k *mqlPythonPackage) name() (string, error) {
    51  	k.populateData()
    52  	return k.Name.Data, nil
    53  }
    54  
    55  func (k *mqlPythonPackage) version() (string, error) {
    56  	k.populateData()
    57  	return k.Version.Data, nil
    58  }
    59  
    60  func (k *mqlPythonPackage) license() (string, error) {
    61  	k.populateData()
    62  	return k.License.Data, nil
    63  }
    64  
    65  func (k *mqlPythonPackage) author() (string, error) {
    66  	k.populateData()
    67  	return k.Author.Data, nil
    68  }
    69  
    70  func (k *mqlPythonPackage) summary() (string, error) {
    71  	k.populateData()
    72  	return k.Summary.Data, nil
    73  }
    74  
    75  func (k *mqlPythonPackage) dependencies() ([]interface{}, error) {
    76  	k.populateData()
    77  	return k.Dependencies.Data, nil
    78  }
    79  
    80  func (k *mqlPythonPackage) populateData() error {
    81  	file := k.GetFile()
    82  	if file.Error != nil {
    83  		return file.Error
    84  	}
    85  	mqlFile := file.Data
    86  	conn := k.MqlRuntime.Connection.(shared.Connection)
    87  	afs := &afero.Afero{Fs: conn.FileSystem()}
    88  	metadataPath := mqlFile.Path.Data
    89  	ppd, err := parseMIME(afs, metadataPath)
    90  	if err != nil {
    91  		return fmt.Errorf("error parsing python package data: %s", err)
    92  	}
    93  
    94  	deps := make([]interface{}, len(ppd.dependencies))
    95  	for i, dep := range ppd.dependencies {
    96  		deps[i] = dep
    97  	}
    98  
    99  	k.Name = plugin.TValue[string]{Data: ppd.name, State: plugin.StateIsSet}
   100  	k.Version = plugin.TValue[string]{Data: ppd.version, State: plugin.StateIsSet}
   101  	k.Author = plugin.TValue[string]{Data: ppd.author, State: plugin.StateIsSet}
   102  	k.Summary = plugin.TValue[string]{Data: ppd.summary, State: plugin.StateIsSet}
   103  	k.License = plugin.TValue[string]{Data: ppd.license, State: plugin.StateIsSet}
   104  	k.Dependencies = plugin.TValue[[]interface{}]{Data: deps, State: plugin.StateIsSet}
   105  
   106  	return nil
   107  }