github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/ociinstaller/mediatypes.go (about)

     1  package ociinstaller
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  
     7  	"github.com/turbot/steampipe/pkg/constants"
     8  	"github.com/turbot/steampipe/pkg/utils"
     9  )
    10  
    11  // Steampipe Media Types
    12  const (
    13  	MediaTypeConfig = "application/vnd.turbot.steampipe.config.v1+json"
    14  
    15  	//deprecate this....
    16  	MediaTypePluginConfig = "application/vnd.turbot.steampipe.plugin.config.v1+json"
    17  
    18  	MediaTypePluginDarwinAmd64Layer  = "application/vnd.turbot.steampipe.plugin.darwin-amd64.layer.v1+gzip"
    19  	MediaTypePluginLinuxAmd64Layer   = "application/vnd.turbot.steampipe.plugin.linux-amd64.layer.v1+gzip"
    20  	MediaTypePluginWindowsAmd64Layer = "application/vnd.turbot.steampipe.plugin.windows-amd64.layer.v1+gzip"
    21  	MediaTypePluginDarwinArm64Layer  = "application/vnd.turbot.steampipe.plugin.darwin-arm64.layer.v1+gzip"
    22  	MediaTypePluginLinuxArm64Layer   = "application/vnd.turbot.steampipe.plugin.linux-arm64.layer.v1+gzip"
    23  	MediaTypePluginWindowsArm64Layer = "application/vnd.turbot.steampipe.plugin.windows-arm64.layer.v1+gzip"
    24  	MediaTypePluginLicenseLayer      = "application/vnd.turbot.steampipe.plugin.license.layer.v1+text"
    25  	MediaTypePluginDocsLayer         = "application/vnd.turbot.steampipe.plugin.docs.layer.v1+tar"
    26  	MediaTypePluginSpcLayer          = "application/vnd.turbot.steampipe.plugin.spc.layer.v1+tar"
    27  
    28  	MediaTypeDbDarwinAmd64Layer  = "application/vnd.turbot.steampipe.db.darwin-amd64.layer.v1+tar"
    29  	MediaTypeDbLinuxAmd64Layer   = "application/vnd.turbot.steampipe.db.linux-amd64.layer.v1+tar"
    30  	MediaTypeDbWindowsAmd64Layer = "application/vnd.turbot.steampipe.db.windows-amd64.layer.v1+tar"
    31  	MediaTypeDbDarwinArm64Layer  = "application/vnd.turbot.steampipe.db.darwin-arm64.layer.v1+tar"
    32  	MediaTypeDbLinuxArm64Layer   = "application/vnd.turbot.steampipe.db.linux-arm64.layer.v1+tar"
    33  	MediaTypeDbWindowsArm64Layer = "application/vnd.turbot.steampipe.db.windows-arm64.layer.v1+tar"
    34  	MediaTypeDbDocLayer          = "application/vnd.turbot.steampipe.db.doc.layer.v1+text"
    35  	MediaTypeDbLicenseLayer      = "application/vnd.turbot.steampipe.db.license.layer.v1+text"
    36  
    37  	MediaTypeFdwDarwinAmd64Layer  = "application/vnd.turbot.steampipe.fdw.darwin-amd64.layer.v1+gzip"
    38  	MediaTypeFdwLinuxAmd64Layer   = "application/vnd.turbot.steampipe.fdw.linux-amd64.layer.v1+gzip"
    39  	MediaTypeFdwWindowsAmd64Layer = "application/vnd.turbot.steampipe.fdw.windows-amd64.layer.v1+gzip"
    40  	MediaTypeFdwDarwinArm64Layer  = "application/vnd.turbot.steampipe.fdw.darwin-arm64.layer.v1+gzip"
    41  	MediaTypeFdwLinuxArm64Layer   = "application/vnd.turbot.steampipe.fdw.linux-arm64.layer.v1+gzip"
    42  	MediaTypeFdwWindowsArm64Layer = "application/vnd.turbot.steampipe.fdw.windows-arm64.layer.v1+gzip"
    43  	MediaTypeFdwDocLayer          = "application/vnd.turbot.steampipe.fdw.doc.layer.v1+text"
    44  	MediaTypeFdwLicenseLayer      = "application/vnd.turbot.steampipe.fdw.license.layer.v1+text"
    45  
    46  	MediaTypeFdwControlLayer = "application/vnd.turbot.steampipe.fdw.control.layer.v1+text"
    47  	MediaTypeFdwSqlLayer     = "application/vnd.turbot.steampipe.fdw.sql.layer.v1+text"
    48  
    49  	MediaTypeAssetReportLayer = "application/vnd.turbot.steampipe.assets.report.layer.v1+tar"
    50  )
    51  
    52  // MediaTypeForPlatform returns media types for binaries for this OS and architecture
    53  // and it's fallbacks in order of priority
    54  func MediaTypeForPlatform(imageType ImageType) ([]string, error) {
    55  	layerFmtGzip := "application/vnd.turbot.steampipe.%s.%s-%s.layer.v1+gzip"
    56  	layerFmtTar := "application/vnd.turbot.steampipe.%s.%s-%s.layer.v1+tar"
    57  
    58  	arch := runtime.GOARCH
    59  	switch imageType {
    60  	case ImageTypeDatabase:
    61  		return []string{fmt.Sprintf(layerFmtTar, imageType, runtime.GOOS, arch)}, nil
    62  	case ImageTypeFdw:
    63  		// detect the underlying architecture(amd64/arm64)
    64  		// we have to do this rather than just using runtime.GOARCH, because runtime.GOARCH does not give us
    65  		// the actual underlying architecture of the system(GOARCH can be changed during runtime)
    66  		arch, err := utils.UnderlyingArch()
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  		return []string{fmt.Sprintf(layerFmtGzip, imageType, runtime.GOOS, arch)}, nil
    71  	case ImageTypePlugin:
    72  		pluginMediaTypes := []string{fmt.Sprintf(layerFmtGzip, imageType, runtime.GOOS, arch)}
    73  		if runtime.GOOS == constants.OSDarwin && arch == constants.ArchARM64 {
    74  			// add the amd64 layer as well, so that we can fall back to it
    75  			// this is required for plugins which don't have an arm64 build yet
    76  			pluginMediaTypes = append(pluginMediaTypes, fmt.Sprintf(layerFmtGzip, imageType, runtime.GOOS, constants.ArchAMD64))
    77  		}
    78  		return pluginMediaTypes, nil
    79  	}
    80  	// there are cases(dashboard commands) where we have a different imageType, we need to return empty
    81  	// in such cases and not return error
    82  	return []string{}, nil
    83  }
    84  
    85  // SharedMediaTypes returns media types that are NOT specific to the os and arch (readmes, control files, etc)
    86  func SharedMediaTypes(imageType ImageType) []string {
    87  	switch imageType {
    88  	case ImageTypeAssets:
    89  		return []string{MediaTypeAssetReportLayer}
    90  	case ImageTypeDatabase:
    91  		return []string{MediaTypeDbDocLayer, MediaTypeDbLicenseLayer}
    92  	case ImageTypeFdw:
    93  		return []string{MediaTypeFdwDocLayer, MediaTypeFdwLicenseLayer, MediaTypeFdwControlLayer, MediaTypeFdwSqlLayer}
    94  	case ImageTypePlugin:
    95  		return []string{MediaTypePluginDocsLayer, MediaTypePluginSpcLayer, MediaTypePluginLicenseLayer}
    96  	}
    97  	return nil
    98  }
    99  
   100  // ConfigMediaTypes :: returns media types for OCI $config data ( in the config, not a layer)
   101  func ConfigMediaTypes() []string {
   102  	return []string{MediaTypeConfig, MediaTypePluginConfig}
   103  }