#Linux disk CLI tool

32 messages · Page 1 of 1 (latest)

gloomy beacon
#

I checked the lfs_core docs and tried out the function read_mounts() which seems to spit out a bunch of info.

    let dev = read_mounts();
    println!("{:?}", dev);

so this seems like a step in the right direction.

#

the signature of the ``read_mounts()function looks like thispub fn read_mounts(options: &ReadOptions) -> Result<Vec<Mount>, Error>So this means it assigns a vector todev` right?

brave moon
#

How do I access the name field of the struct and assign it to a variable? My thought was to use let dev = lfs_core::BlockDevice::name; but that's not working.
To strictly answer the question, to "access and assign it to a variable", you start with a value of that type and do let dev = variable.name; (in this case that'll move name out of variable. If you'd rather not move it, you can do a reference, let dev = &*variable.name;)

Do you already have a value of BlockDevice, or are you trying to make a new value of it?

#

If your goal was to make a new one, then you can just (or call a method or whatever which returns this type)

let dev = BlockDevice {
    name: "something".to_owned(),
    dm_name: None,
    // or DeviceId::new(0, 0)
    id: DeviceId { major: 0, minor: 0 },
    parent: None
};
#

(Obviously I've just stubbed out those for None. You may want to actually fill it, e.g. with Some(whatever))

#

I figure one of those 2 answers will probably cover your question 😅

gloomy beacon
#

yup it's enough for me to play around with at the moment! thank you!

#

I'm going to do a little refresher on accessing struct fields in the book

gloomy beacon
#

didn't get backto this as soon as I had hoped for. But I think I want to try and play around with this Mount struct

#

my thought process is I should be able to assign the struct to a variable let dev = lfs_core::Mount and then obtain the info field by doing dev.info

is my understanding correct?

#

I think the challenge I'm having is working with a crate's struct instead of creating the struct myself as I've done from the book and other exercises

brave moon
# gloomy beacon my thought process is I should be able to assign the struct to a variable `let d...

I should be able to assign the struct to a variable let dev = lfs_core::Mount
Well, that depends on their api at the end of the day.

Sometimes there's an associated new() fn on the struct, sometimes you make it directly like you said, and sometimes there's other api which returns it.

and then obtain the info field by doing dev.info
Once you have a value of this type, that's right yeah (if it's a pub field; if not, then they might have some kind of accessor function to get the value)

brave moon
gloomy beacon
#

ah I see now. Cool I'll look into it more tonight, Thanks!

gloomy beacon
#

update on this, I switched to just using std::Process::Command to invoke lsblk with flags.

#

I think the problem I was having with lfs_core was I assumed the struct was created already so I was trying to access fields that didn't exist. Also the structs I wanted to use didn't have any constructors and I couldn't figure out how to make a new one.

#

going to use clap next to expand the table to include columns based on arguments given to the tool.

gloomy beacon
#

also going to try and use lsblk -j for JSON formatting to make extensibility easier

dire summit
#

on a struct that wasnt instantiated

#

so yeah first step is get an actual struct

#

then use .name (access field/self methods) not ::name (access method/const)

gloomy beacon
#

yeah, I'm planning on using structs for the JSON stuff so i'll get some struct exercise with that

dire summit
#

are they deeply nested, because if not, you can try toml

#

and if you want json i suggest serde and serde_json

#

for toml i suggest serde and toml

gloomy beacon
#

serde_json is what I was looking at. I was going to use JSON because lsblk has a flag for outputting disk information in JSON format

dire summit
#

just so yk with serde its really easy to make both toml and json with the same struct

#

so just 1 struct writes to both json and toml