internal/fetcher/channel.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | package fetcher import ( "context" "fmt" "os/exec" "path/filepath" "strconv" "strings" "time" "github.com/Southclaws/fault" "github.com/Southclaws/fault/fmsg" "alin.ovh/searchix/internal/config" "alin.ovh/searchix/internal/index" ) type ChannelFetcher struct { Source *config.Source SourceFile string *Options } func NewChannelFetcher( source *config.Source, options *Options, ) (*ChannelFetcher, error) { switch source.Importer { case config.Options: return &ChannelFetcher{ Source: source, Options: options, }, nil default: return nil, fault.Newf("unsupported importer type %s", source.Importer) } } func (i *ChannelFetcher) FetchIfNeeded( ctx context.Context, sourceMeta *index.SourceMeta, ) (*FetchedFiles, error) { target := i.Source.JoinPath("options.json") args := []string{ "--no-build-output", "--timeout", strconv.Itoa(int(i.Source.Timeout.Seconds() - 1)), fmt.Sprintf("<%s/%s>", i.Source.Channel, i.Source.ImportPath), "--attr", i.Source.Attribute, "--no-out-link", } if i.Source.URL != "" { args = append(args, "-I", fmt.Sprintf("%s=%s", i.Source.Channel, i.Source.URL)) } i.Logger.Debug("nix-build command", "args", args) cmd := exec.CommandContext(ctx, "nix-build", args...) out, err := cmd.Output() if err != nil { return nil, fault.Wrap(err, fmsg.With("failed to run nix-build (--dry-run)")) } //nolint:forbidigo // nix-build only gives the top-level path outPath := filepath.Join(strings.TrimSpace(string(out)), i.Source.OutputPath, "options.json") i.Logger.Debug( "checking output path", "outputPath", outPath, "updated", outPath != sourceMeta.Path, ) if outPath != sourceMeta.Path { sourceMeta.Path = outPath sourceMeta.UpdatedAt = time.Now().Truncate(time.Second) if exists, err := i.Root.Exists(target); err != nil { return nil, fault.Wrap(err, fmsg.With("failed to check if target path exists")) } else if exists { err := i.Root.Remove(target) if err != nil { return nil, fault.Wrap(err, fmsg.Withf("failed to remove previous file %s", target)) } } err := i.Root.CopyTo(outPath, target) if err != nil { return nil, fault.Wrap(err, fmsg.With("failed to copy options.json")) } err = i.Root.Chtimes(target, time.Time{}, sourceMeta.UpdatedAt) if err != nil { return nil, fault.Wrap(err, fmsg.With("failed to update timestamps")) } } file, err := i.Root.Open(target) if err != nil { return nil, fault.Wrap(err, fmsg.With("failed to open options.json")) } return &FetchedFiles{ Options: file, }, nil } |