Debian Codegraph @

main.go

raw symbols SCIP
1 package main
2
3 import (
4 "fmt"
5 "net/http"
6 "os"
7
8 "github.com/prometheus/client_golang/prometheus/promhttp"
9 "github.com/sirupsen/logrus"
10 "github.com/xperimental/nextcloud-exporter/internal/client"
11 "github.com/xperimental/nextcloud-exporter/internal/config"
12 "github.com/xperimental/nextcloud-exporter/internal/login"
13 "github.com/xperimental/nextcloud-exporter/internal/metrics"
14 "github.com/xperimental/nextcloud-exporter/serverinfo"
15 )
16
17 var (
18 // Version contains the version as set during the build.
19 Version = ""
20
21 // GitCommit contains the git commit hash set during the build.
22 GitCommit = ""
23
24 log = &logrus.Logger{
25 Out: os.Stderr,
26 Formatter: &logrus.TextFormatter{
27 DisableTimestamp: true,
28 },
29 Hooks: make(logrus.LevelHooks),
30 Level: logrus.InfoLevel,
31 ExitFunc: os.Exit,
32 ReportCaller: false,
33 }
34 )
35
36 func main() {
37 cfg, err := config.Get()
38 if err != nil {
39 log.Fatalf("Error loading configuration: %s", err)
40 }
41
42 if cfg.RunMode == config.RunModeHelp {
43 return
44 }
45
46 if cfg.RunMode == config.RunModeVersion {
47 fmt.Println(Version)
48 return
49 }
50
51 log.Infof("nextcloud-exporter %s", Version)
52 userAgent := fmt.Sprintf("nextcloud-exporter/%s", Version)
53
54 if cfg.RunMode == config.RunModeLogin {
55 if cfg.ServerURL == "" {
56 log.Fatalf("Need to specify --server for login.")
57 }
58 loginClient := login.Init(log, userAgent, cfg.ServerURL, cfg.TLSSkipVerify)
59
60 log.Infof("Starting interactive login on: %s", cfg.ServerURL)
61 if err := loginClient.StartInteractive(); err != nil {
62 log.Fatalf("Error during login: %s", err)
63 }
64 return
65 }
66
67 if err := cfg.Validate(); err != nil {
68 log.Fatalf("Invalid configuration: %s", err)
69 }
70
71 if cfg.AuthToken == "" {
72 log.Infof("Nextcloud server: %s User: %s", cfg.ServerURL, cfg.Username)
73 } else {
74 log.Infof("Nextcloud server: %s Authentication using token.", cfg.ServerURL)
75 }
76
77 infoURL := serverinfo.InfoURL(cfg.ServerURL, !cfg.Info.Apps, !cfg.Info.Update)
78
79 if cfg.TLSSkipVerify {
80 log.Warn("HTTPS certificate verification is disabled.")
81 }
82
83 infoClient := client.New(infoURL, cfg.Username, cfg.Password, cfg.AuthToken, cfg.Timeout, userAgent, cfg.TLSSkipVerify)
84 if err := metrics.RegisterCollector(log, infoClient, cfg.Info.Apps, cfg.Info.Update); err != nil {
85 log.Fatalf("Failed to register collector: %s", err)
86 }
87
88 if err := metrics.RegisterInfoMetric(Version, GitCommit); err != nil {
89 log.Fatalf("Failed to register info metric: %s", err)
90 }
91
92 http.Handle("/metrics", promhttp.Handler())
93 http.Handle("/", http.RedirectHandler("/metrics", http.StatusFound))
94
95 log.Infof("Listen on %s...", cfg.ListenAddr)
96 log.Fatal(http.ListenAndServe(cfg.ListenAddr, nil))
97 }