Skip to content

Commit

Permalink
add clippy check to ci
Browse files Browse the repository at this point in the history
  • Loading branch information
Janik-Haag committed Feb 16, 2024
1 parent 1429082 commit e9a6d11
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 64 deletions.
39 changes: 23 additions & 16 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use clap::{Parser, Subcommand};
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
// use tvix_serde;
use std::net::IpAddr;
use std::{env, path::PathBuf};
use std::error;
use std::fs;
use std::io::Write;
use std::net::IpAddr;
use std::{env, path::PathBuf};

#[derive(Parser, Debug)]
#[clap(version)]
Expand All @@ -32,9 +32,9 @@ enum SubCommands {
#[derive(Debug, Serialize, Deserialize)]
struct IpAddresses {
address: IpAddr, // TODO(Janik): make it use CIDR
status: String, // TODO(Janik): enum actual netbox values
status: String, // TODO(Janik): enum actual netbox values
dns_name: String,
tags: Vec<String>
tags: Vec<String>,
}

fn netbox_api_request(url: String) -> Result<String, Box<dyn error::Error>> {
Expand All @@ -43,37 +43,44 @@ fn netbox_api_request(url: String) -> Result<String, Box<dyn error::Error>> {
let client = reqwest::blocking::Client::new();

// We can't use the bearer auth method because netbox doesn't comply with the standards.
let resp = client.get(url).header("Authorization", "Token ".to_owned() + &token).send()?;
let resp = client
.get(url)
.header("Authorization", "Token ".to_owned() + &token)
.send()?;
let json = resp.json::<serde_json::Value>()?;
Ok(json.to_string())
}

#[derive(Serialize, Deserialize, Debug)]
struct Nix {
a: String,
b: u32
b: u32,
}

// this is currently stuck on https://cl.tvl.fyi/c/depot/+/10581
// because we need import functionality to pull in and eval the users config
// the code here is just a filler example to make it compile happily
fn get_current_config() -> Result<Nix, Box<dyn error::Error>> {
let deserialized: Result<Nix, Box<dyn error::Error>> = Ok(tvix_serde::from_str::<Nix>("lib.attrsets.recursiveUpdate { a = \"coding with tvix!\"; } { b = 231; }")?);
deserialized
}
// fn get_current_config() -> Result<Nix, Box<dyn error::Error>> {
// let deserialized: Result<Nix, Box<dyn error::Error>> = Ok(tvix_serde::from_str::<Nix>("lib.attrsets.recursiveUpdate { a = \"coding with tvix!\"; } { b = 231; }")?);
// deserialized
// }

fn add(ips: bool) -> Result<(), Box<dyn error::Error>> {
if ips {
}
fn add(_ips: bool) -> Result<(), Box<dyn error::Error>> {
// if ips {}
Ok(())
}

fn export(ips: bool, location: Option<PathBuf>) -> Result<(), Box<dyn error::Error>>{
fn export(ips: bool, location: Option<PathBuf>) -> Result<(), Box<dyn error::Error>> {
let location = location.unwrap_or_else(|| PathBuf::from("/tmp/nixbox_export"));
fs::create_dir_all(location.clone())?;
if ips {
let mut ips_file = fs::File::create(location.to_str().unwrap().to_owned() + "ips.json")?;
let _ = ips_file.write_all((netbox_api_request(String::from("https://demo.netbox.dev/api/ipam/ip-addresses"))?).as_bytes());
let _ = ips_file.write_all(
(netbox_api_request(String::from(
"https://demo.netbox.dev/api/ipam/ip-addresses",
))?)
.as_bytes(),
);
}
Ok(())
}
Expand Down
115 changes: 67 additions & 48 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,87 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-compat.url = "github:edolstra/flake-compat";
flake-compat.flake = false;
flake-utils.url = "github:numtide/flake-utils";

crane.url = "github:ipetkov/crane";
crane.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = { self, nixpkgs, flake-compat, crane }:
let
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
in
{
packages = forAllSystems (system:
let
pkgs = nixpkgs.legacyPackages.${system};
craneLib = crane.lib.${system};
in
rec {
default = cli;
dataSourceDemo = (import ./utils/builder.nix { inherit (nixpkgs) lib; pkgs = nixpkgs.legacyPackages.${system}; }).dataSource { };
cli = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./cli);
strictDeps = true;
outputs = { self, nixpkgs, flake-compat, flake-utils, crane }:
(flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
craneLib = crane.lib.${system};

# Needed to get openssl-sys to use pkg-config.
OPENSSL_NO_VENDOR = 1;
# Common derivation arguments used for all builds
cliCommonArgs = {
src = craneLib.cleanCargoSource (craneLib.path ./cli);
strictDeps = true;

buildInputs = with pkgs; [
openssl
];
# Needed to get openssl-sys to use pkg-config.

nativeBuildInputs = with pkgs; [
pkg-config
];
OPENSSL_NO_VENDOR = 1;

buildInputs = with pkgs; [
openssl
];

nativeBuildInputs = with pkgs; [
pkg-config
];
};
cliCargoArtifacts = craneLib.buildDepsOnly (cliCommonArgs // {
pname = "nixbox-cli-deps";
});
cliClippy = craneLib.cargoClippy (cliCommonArgs // {
cargoArtifacts = cliCargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});

cliCrate = craneLib.buildPackage (cliCommonArgs // {
cargoArtifacts = cliCargoArtifacts;
});

cliCoverage = craneLib.cargoTarpaulin (cliCommonArgs // {
cargoArtifacts = cliCargoArtifacts;
});
in
{
packages =
{
default = cliCrate;
cli = cliCrate;
dataSourceDemo = (import ./utils/builder.nix { inherit (nixpkgs) lib; pkgs = nixpkgs.legacyPackages.${system}; }).dataSource { };
};
}
);
utils = import ./utils { inherit (nixpkgs) lib; };
nixosModules = rec {
nixbox = import ./modules/nixbox.nix { inherit (self) utils; };
default = nixbox;
};
formatter = forAllSystems (
system:
nixpkgs.legacyPackages.${system}.nixpkgs-fmt
);
tests = nixpkgs.lib.mapAttrs (name: v: import "${./utils}/tests/${name}.nix" { inherit self; inherit (nixpkgs) lib; inherit (self) utils; }) (builtins.removeAttrs self.utils [ "__unfix__" "extend" "generate" ]);
devShells = forAllSystems (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
default = pkgs.mkShell {
buildInputs = with pkgs; [
formatter = nixpkgs.legacyPackages.${system}.nixpkgs-fmt
;
devShells = {
default = craneLib.devShell {
checks = self.checks.${system};
inputsFrom = [
cliCrate
];
packages = with pkgs; [
nixpkgs-fmt
nix-unit
nixdoc
statix

# nixbox-cli
cargo
clippy
];
};
});
};
checks = {
inherit
cliCrate
cliClippy
# cliCoverage // not yet used
;
};
})) // {
tests = nixpkgs.lib.mapAttrs (name: v: import "${./utils}/tests/${name}.nix" { inherit self; inherit (nixpkgs) lib; inherit (self) utils; }) (builtins.removeAttrs self.utils [ "__unfix__" "extend" "generate" ]);
utils = import ./utils { inherit (nixpkgs) lib; };
nixosModules = rec {
nixbox = import ./modules/nixbox.nix { inherit (self) utils; };
default = nixbox;
};
};
}

0 comments on commit e9a6d11

Please sign in to comment.