github.com/rpdict/ponzu@v0.10.1-0.20190226054626-477f29d6bf5e/system/db/addon.go (about)

     1  package db
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"errors"
     7  	"fmt"
     8  	"log"
     9  	"net/url"
    10  
    11  	"github.com/boltdb/bolt"
    12  	"github.com/gorilla/schema"
    13  )
    14  
    15  var (
    16  	// ErrNoAddonExists indicates that there was not addon found in the db
    17  	ErrNoAddonExists = errors.New("No addon exists.")
    18  )
    19  
    20  // Addon looks for an addon by its addon_reverse_dns as the key and returns
    21  // the []byte as json representation of an addon
    22  func Addon(key string) ([]byte, error) {
    23  	buf := &bytes.Buffer{}
    24  
    25  	err := store.View(func(tx *bolt.Tx) error {
    26  		b := tx.Bucket([]byte("__addons"))
    27  		if b == nil {
    28  			return bolt.ErrBucketNotFound
    29  		}
    30  
    31  		val := b.Get([]byte(key))
    32  
    33  		if val == nil {
    34  			return ErrNoAddonExists
    35  		}
    36  
    37  		_, err := buf.Write(val)
    38  		if err != nil {
    39  			return err
    40  		}
    41  
    42  		return nil
    43  	})
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return buf.Bytes(), nil
    49  }
    50  
    51  // SetAddon stores the values of an addon into the __addons bucket with a the
    52  // `addon_reverse_dns` field used as the key. `kind` is the interface{} type for
    53  // the provided addon (as in the result of calling addon.Types[id])
    54  func SetAddon(data url.Values, kind interface{}) error {
    55  	dec := schema.NewDecoder()
    56  	dec.IgnoreUnknownKeys(true)
    57  	dec.SetAliasTag("json")
    58  	err := dec.Decode(kind, data)
    59  
    60  	v, err := json.Marshal(kind)
    61  
    62  	k := data.Get("addon_reverse_dns")
    63  	if k == "" {
    64  		name := data.Get("addon_name")
    65  		return fmt.Errorf(`Addon "%s" has no identifier to use as key.`, name)
    66  	}
    67  
    68  	err = store.Update(func(tx *bolt.Tx) error {
    69  		b := tx.Bucket([]byte("__addons"))
    70  		if b == nil {
    71  			return bolt.ErrBucketNotFound
    72  		}
    73  
    74  		err := b.Put([]byte(k), v)
    75  		if err != nil {
    76  			return err
    77  		}
    78  
    79  		return nil
    80  	})
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  // AddonAll returns all registered addons as a [][]byte
    89  func AddonAll() [][]byte {
    90  	var all [][]byte
    91  
    92  	err := store.View(func(tx *bolt.Tx) error {
    93  		b := tx.Bucket([]byte("__addons"))
    94  		if b == nil {
    95  			return bolt.ErrBucketNotFound
    96  		}
    97  
    98  		err := b.ForEach(func(k, v []byte) error {
    99  			all = append(all, v)
   100  
   101  			return nil
   102  		})
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		return nil
   108  	})
   109  	if err != nil {
   110  		log.Println("Error finding addons in db with db.AddonAll:", err)
   111  		return nil
   112  	}
   113  
   114  	return all
   115  }
   116  
   117  // DeleteAddon removes an addon from the db by its key, the addon_reverse_dns
   118  func DeleteAddon(key string) error {
   119  	err := store.Update(func(tx *bolt.Tx) error {
   120  		b := tx.Bucket([]byte("__addons"))
   121  		if b == nil {
   122  			return bolt.ErrBucketNotFound
   123  		}
   124  
   125  		if err := b.Delete([]byte(key)); err != nil {
   126  			return err
   127  		}
   128  
   129  		return nil
   130  	})
   131  	if err != nil {
   132  		return err
   133  	}
   134  
   135  	return nil
   136  }
   137  
   138  // AddonExists checks if there is an existing addon stored. The key is an the
   139  // value at addon_reverse_dns
   140  func AddonExists(key string) bool {
   141  	var exists bool
   142  
   143  	if store == nil {
   144  		Init()
   145  	}
   146  
   147  	err := store.Update(func(tx *bolt.Tx) error {
   148  		b, err := tx.CreateBucketIfNotExists([]byte("__addons"))
   149  		if err != nil {
   150  			return err
   151  		}
   152  		if b.Get([]byte(key)) == nil {
   153  			return nil
   154  		}
   155  
   156  		exists = true
   157  		return nil
   158  	})
   159  	if err != nil {
   160  		log.Println("Error checking existence of addon with key:", key, "-", err)
   161  		return false
   162  	}
   163  
   164  	return exists
   165  }