write go manifest representation
This commit is contained in:
parent
b703b09f76
commit
d52447dc23
79
api/capability.go
Normal file
79
api/capability.go
Normal file
@ -0,0 +1,79 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Capability interface {
|
||||
ID() string
|
||||
}
|
||||
type CapabilityJSON struct {
|
||||
Capability
|
||||
}
|
||||
type BasicCapability struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
func (c BasicCapability) ID() string {
|
||||
return c.Id
|
||||
}
|
||||
|
||||
const CapabilityBasic string = "basic"
|
||||
|
||||
type DBusCapability struct {
|
||||
Id string `json:"id"`
|
||||
Own []string `json:"own,omitempty"`
|
||||
}
|
||||
|
||||
func (c DBusCapability) ID() string {
|
||||
return c.Id
|
||||
}
|
||||
|
||||
const CapabilityDBus = "dbus"
|
||||
|
||||
type capabilityType struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
func (c *CapabilityJSON) MarshalJSON() ([]byte, error) {
|
||||
if c == nil || c.Capability == nil {
|
||||
return nil, fmt.Errorf("invalid capability")
|
||||
}
|
||||
var v any
|
||||
switch cv := c.Capability.(type) {
|
||||
case *BasicCapability:
|
||||
v = &struct {
|
||||
capabilityType
|
||||
*BasicCapability
|
||||
}{capabilityType{CapabilityBasic}, cv}
|
||||
|
||||
case *DBusCapability:
|
||||
v = &struct {
|
||||
capabilityType
|
||||
*DBusCapability
|
||||
}{capabilityType{CapabilityDBus}, cv}
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid capability")
|
||||
}
|
||||
return json.Marshal(v)
|
||||
}
|
||||
func (c *CapabilityJSON) UnmarshalJSON(data []byte) error {
|
||||
t := new(capabilityType)
|
||||
if err := json.Unmarshal(data, &t); err != nil {
|
||||
return err
|
||||
}
|
||||
switch t.Type {
|
||||
case CapabilityBasic:
|
||||
*c = CapabilityJSON{new(BasicCapability)}
|
||||
|
||||
case CapabilityDBus:
|
||||
*c = CapabilityJSON{new(DBusCapability)}
|
||||
|
||||
default:
|
||||
return fmt.Errorf("invalid capability")
|
||||
}
|
||||
|
||||
return json.Unmarshal(data, c.Capability)
|
||||
}
|
||||
25
api/manifest.go
Normal file
25
api/manifest.go
Normal file
@ -0,0 +1,25 @@
|
||||
package api
|
||||
|
||||
import "hakurei.app/container/check"
|
||||
|
||||
type Manifest struct {
|
||||
Metadata Metadata `json:"metadata"`
|
||||
Executable Executable `json:"executable"`
|
||||
Capabilities []CapabilityJSON `json:"capabilities"`
|
||||
Permissions []CapabilityJSON `json:"permissions"`
|
||||
}
|
||||
type Metadata struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Author string `json:"author"`
|
||||
Icon *check.Absolute `json:"icon"`
|
||||
Version int `json:"version"`
|
||||
VersionName string `json:"version_name"`
|
||||
}
|
||||
type Executable struct {
|
||||
BaseImage string `json:"base_image"`
|
||||
Binary *check.Absolute `json:"binary"`
|
||||
Args []string `json:"args"`
|
||||
Env map[string]string `json:"env"`
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user