github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/spyglass/lenses/fake/fake.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package fake
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"io"
    23  )
    24  
    25  type Artifact struct {
    26  	Path    string
    27  	Content []byte
    28  	Meta    map[string]string
    29  	Link    *string
    30  }
    31  
    32  func (fa *Artifact) JobPath() string {
    33  	return fa.Path
    34  }
    35  
    36  func (fa *Artifact) Size() (int64, error) {
    37  	return int64(len(fa.Content)), nil
    38  }
    39  
    40  func (fa *Artifact) Metadata() (map[string]string, error) {
    41  	return fa.Meta, nil
    42  }
    43  
    44  func (fa *Artifact) UpdateMetadata(now map[string]string) error {
    45  	fa.Meta = now
    46  	return nil
    47  }
    48  
    49  const NotFound = "linknotfound.io/404"
    50  
    51  func (fa *Artifact) CanonicalLink() string {
    52  	if fa.Link == nil {
    53  		return NotFound
    54  	}
    55  	return *fa.Link
    56  }
    57  
    58  func (fa *Artifact) ReadAt(b []byte, off int64) (int, error) {
    59  	r := bytes.NewReader(fa.Content)
    60  	return r.ReadAt(b, off)
    61  }
    62  
    63  func (fa *Artifact) ReadAll() ([]byte, error) {
    64  	r := bytes.NewReader(fa.Content)
    65  	return io.ReadAll(r)
    66  }
    67  
    68  func (fa *Artifact) ReadTail(n int64) ([]byte, error) {
    69  	size, err := fa.Size()
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	buf := make([]byte, n)
    74  	_, err = fa.ReadAt(buf, size-n)
    75  	return buf, err
    76  }
    77  
    78  func (fa *Artifact) UseContext(ctx context.Context) error {
    79  	return nil
    80  }
    81  
    82  func (fa *Artifact) ReadAtMost(n int64) ([]byte, error) {
    83  	buf := make([]byte, n)
    84  	_, err := fa.ReadAt(buf, 0)
    85  	return buf, err
    86  }