github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/bom/docker/pkgmgr.go (about)

     1  /*
     2   * Copyright (c) 2021 CodeNotary, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package docker
    10  
    11  import (
    12  	"errors"
    13  
    14  	"github.com/vchain-us/vcn/pkg/bom/artifact"
    15  	"github.com/vchain-us/vcn/pkg/bom/executor"
    16  )
    17  
    18  type pkgManager interface {
    19  	PackageByFile(e executor.Executor, file string, output artifact.OutputOptions) (artifact.Dependency, error)
    20  	AllPackages(e executor.Executor, output artifact.OutputOptions) ([]artifact.Dependency, error)
    21  	Type() string
    22  }
    23  
    24  var ErrNotFound = errors.New("not found")
    25  
    26  func probePackageManager(e executor.Executor) (pkgManager, error) {
    27  	_, _, exitCode, err := e.Exec([]string{"apk", "--version"})
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	if exitCode == 0 {
    32  		return &apk{}, nil
    33  	}
    34  
    35  	_, _, exitCode, err = e.Exec([]string{"dpkg", "--version"})
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	if exitCode == 0 {
    40  		return &dpkg{}, nil
    41  	}
    42  
    43  	_, _, exitCode, err = e.Exec([]string{"rpm", "--version"})
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	if exitCode == 0 {
    48  		return &rpm{}, nil
    49  	}
    50  
    51  	return nil, nil // cannot identify
    52  }