Split fetch from jobsearch into a separate fetcher command, and renamed fetch once to fts-once
job-search no longer has a fetch command. Its subcommands are now in cmd/fetcher, which reads the same configuration: "fetcher service" runs every enabled source's fetcher on its interval until stopped, and "fetcher fts-once" (was "fetch once") fetches from Find a Tender once and prints the releases as JSON. The new namea say what the commands do, because service runs every source while fts-once fetches only from Find a Tender. The code stays in internal/cli/fetch, where NewCommands replaces the fetch parent command. once.go is now fts_once.go and commandOnce is commandFTSOnce. The README's usage, the config README and the find_a_tender api_root comment now give the new command names.
This commit is contained in:
1 parent
5e61c11eb9
commit
ac44b45f06
11 files changed
+155
-44
No files matched your search
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
Copyright © 2026 Craig Williams <craig@avsdev.uk>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
// Command fetcher, for fetching opportunities from JobSearch's sources. Run
|
||||
// `fetcher --help` for usage.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.avsdev.uk/Go/job-search/internal/cli"
|
||||
"git.avsdev.uk/Go/job-search/internal/cli/fetch"
|
||||
)
|
||||
|
||||
// newRootCmd builds the command tree. It returns a fresh tree on each call so
|
||||
// tests can run commands in isolation.
|
||||
func newRootCmd() *cobra.Command {
|
||||
opts := &cli.Options{}
|
||||
|
||||
root := &cobra.Command{
|
||||
Use: "fetcher",
|
||||
Short: "Fetch tools for JobSearch",
|
||||
Long: `fetcher fetches from Find a Tender once and prints the releases as
|
||||
JSON (fts-once), or polls every enabled source on its interval until stopped
|
||||
(service). It reads the JobSearch configuration.
|
||||
`,
|
||||
SilenceUsage: true, // don't print usage on runtime errors
|
||||
SilenceErrors: true, // main prints the error once
|
||||
CompletionOptions: cobra.CompletionOptions{
|
||||
HiddenDefaultCmd: true,
|
||||
},
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
cli.SetupInitialLogging(cmd)
|
||||
|
||||
slog.Debug("command persistent pre-run", "command", "root")
|
||||
slog.Debug(
|
||||
"command",
|
||||
"path", strings.Join(strings.Split(cmd.CommandPath(), " ")[1:], " "),
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
|
||||
slog.Debug("command persistent post-run", "command", "root")
|
||||
|
||||
return opts.Finish()
|
||||
},
|
||||
}
|
||||
|
||||
cobra.EnableTraverseRunHooks = true
|
||||
|
||||
opts.Bind(root)
|
||||
|
||||
root.AddCommand(fetch.NewCommands(opts)...)
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := newRootCmd().Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "fetcher:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The commands are tested in internal/cli/fetch. This test checks newRootCmd
|
||||
// puts them together.
|
||||
|
||||
func TestCommandTree(t *testing.T) {
|
||||
root := newRootCmd()
|
||||
|
||||
var names []string
|
||||
for _, c := range root.Commands() {
|
||||
names = append(names, c.Name())
|
||||
}
|
||||
|
||||
for _, want := range []string{"fts-once", "service"} {
|
||||
if !slices.Contains(names, want) {
|
||||
t.Errorf("fetcher has no %q command; has %v", want, names)
|
||||
}
|
||||
}
|
||||
|
||||
for _, flag := range []string{"config", "debug", "quiet"} {
|
||||
if root.PersistentFlags().Lookup(flag) == nil {
|
||||
t.Errorf("fetcher has no --%s flag", flag)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user