github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/updater/sources/local.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package sources
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"os"
    10  
    11  	"github.com/keybase/client/go/updater"
    12  	"github.com/keybase/client/go/updater/util"
    13  )
    14  
    15  // LocalUpdateSource finds releases/updates from a path (used primarily for testing)
    16  type LocalUpdateSource struct {
    17  	path     string
    18  	jsonPath string
    19  	log      Log
    20  }
    21  
    22  // NewLocalUpdateSource returns local update source
    23  func NewLocalUpdateSource(path string, jsonPath string, log Log) LocalUpdateSource {
    24  	return LocalUpdateSource{
    25  		path:     path,
    26  		jsonPath: jsonPath,
    27  		log:      log,
    28  	}
    29  }
    30  
    31  // Description is local update source description
    32  func (k LocalUpdateSource) Description() string {
    33  	return "Local"
    34  }
    35  
    36  // FindUpdate returns update for options
    37  func (k LocalUpdateSource) FindUpdate(options updater.UpdateOptions) (*updater.Update, error) {
    38  	jsonFile, err := os.Open(k.jsonPath)
    39  	defer util.Close(jsonFile)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	var update updater.Update
    45  	if err := json.NewDecoder(jsonFile).Decode(&update); err != nil {
    46  		return nil, fmt.Errorf("Invalid update JSON: %s", err)
    47  	}
    48  
    49  	update.Asset.URL = fmt.Sprintf("file://%s", k.path)
    50  	// TODO: Only do if version is newer or forced (this source is used for testing, so ok to hardcode NeedUpdate)
    51  	update.NeedUpdate = true
    52  	k.log.Debugf("Returning update: %#v", update)
    53  	return &update, nil
    54  }